-
Notifications
You must be signed in to change notification settings - Fork 26k
/
[slug].js
59 lines (53 loc) · 1.33 KB
/
[slug].js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import fs from 'fs'
import path from 'path'
import React from 'react'
import Link from 'next/link'
import { useRouter } from 'next/router'
export async function getStaticPaths() {
return {
paths: [
{
params: { slug: 'test-errors-1' },
},
],
fallback: 'blocking',
}
}
export async function getStaticProps({ params }) {
if (params.slug.startsWith('test-errors')) {
const errorFile = path.join(process.cwd(), 'error.txt')
if (fs.existsSync(errorFile)) {
const data = await fs.readFileSync(errorFile, 'utf8')
if (data.trim() === 'yes') {
throw new Error('throwing error for /blocking-fallback/' + params.slug)
}
}
}
await new Promise((resolve) => setTimeout(resolve, 1000))
return {
props: {
params,
hello: 'world',
post: params.slug,
random: Math.random(),
time: (await import('perf_hooks')).performance.now(),
},
revalidate: 1,
}
}
export default ({ post, time, params }) => {
if (useRouter().isFallback) {
return <p>hi fallback</p>
}
return (
<>
<p>Post: {post}</p>
<span id="time">time: {time}</span>
<div id="params">{JSON.stringify(params)}</div>
<div id="query">{JSON.stringify(useRouter().query)}</div>
<Link href="/">
<a id="home">to home</a>
</Link>
</>
)
}