url-minify/frontend/pages/[id].js

60 lines
1.3 KiB
JavaScript
Raw Normal View History

import { useEffect, useState } from 'react'
import Head from 'next/head'
import HomeSection from 'components/HomeSection/homeSection'
import NavBar from 'components/NavBar'
import Features from 'components/Features'
import { useRouter } from 'next/router'
import axios from 'helpers/Axios'
2022-02-13 08:28:33 +00:00
function Redirector(props) {
const router = useRouter()
2022-02-13 08:28:33 +00:00
useEffect(() => {
if (!props?.resData.success) {
// router.push('/404')
} else {
// router.replace(`${props.resData.data.originalUrl}`)
}
}, [])
2022-02-13 08:28:33 +00:00
return (
<div className={''}>
2022-02-13 08:28:33 +00:00
<Head>
<title>URL MiniFy</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main className={'main-bg'}>Not Found</main>
2022-02-13 08:28:33 +00:00
</div>
)
}
2022-02-13 08:28:33 +00:00
Redirector.getInitialProps = async (context) => {
const { id } = context.query
let resData = {}
2022-02-13 08:28:33 +00:00
if (id) {
axios.get(`/minify/${id}/views`).catch((err) => {
console.error(err)
})
await axios
.get(`/minify/alias/${id}`)
2022-02-13 08:28:33 +00:00
.then(function (response) {
resData = response.data
2022-02-13 08:28:33 +00:00
})
.catch(function (error) {
resData = error
})
2022-02-13 08:28:33 +00:00
}
if (context?.res) {
const go = resData?.data?.originalUrl ? resData?.data?.originalUrl : '/404'
console.log(go)
2022-02-13 08:28:33 +00:00
context?.res.writeHead(302, {
Location: go,
})
context?.res.end()
2022-02-13 08:28:33 +00:00
}
return {}
}
2022-02-13 08:28:33 +00:00
export default Redirector