Skip to content

Commit 42b26ad

Browse files
committed
Fix serverless case and add tests for getServerSideProps
1 parent aa3e0dd commit 42b26ad

File tree

5 files changed

+132
-2
lines changed

5 files changed

+132
-2
lines changed

packages/next/build/webpack/loaders/next-serverless-loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ const nextServerlessLoader: loader.Loader = function () {
772772
773773
if (!renderMode) {
774774
if (_nextData || getStaticProps || getServerSideProps) {
775-
if (renderOpts.ssgNotFound) {
775+
if (renderOpts.isNotFound) {
776776
res.statusCode = 404
777777
778778
const NotFoundComponent = ${

packages/next/next-server/server/next-server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,7 @@ export default class Server {
13511351
html = renderResult.html
13521352
pageData = renderResult.renderOpts.pageData
13531353
sprRevalidate = renderResult.renderOpts.revalidate
1354-
isNotFound = renderResult.renderOpts.ssgNotFound
1354+
isNotFound = renderResult.renderOpts.isNotFound
13551355
} else {
13561356
const origQuery = parseUrl(req.url || '', true).query
13571357
const resolvedUrl = formatUrl({
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Link from 'next/link'
2+
import { useRouter } from 'next/router'
3+
4+
export default function Page(props) {
5+
const router = useRouter()
6+
7+
return (
8+
<>
9+
<p id="gssp">gssp page</p>
10+
<p id="props">{JSON.stringify(props)}</p>
11+
<p id="router-query">{JSON.stringify(router.query)}</p>
12+
<p id="router-pathname">{router.pathname}</p>
13+
<p id="router-as-path">{router.asPath}</p>
14+
<Link href="/">
15+
<a id="to-index">to /</a>
16+
</Link>
17+
<br />
18+
</>
19+
)
20+
}
21+
22+
export const getServerSideProps = ({ query }) => {
23+
if (query.hiding) {
24+
return {
25+
unstable_notFound: true,
26+
}
27+
}
28+
29+
return {
30+
props: {
31+
hello: 'world',
32+
},
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Link from 'next/link'
2+
import { useRouter } from 'next/router'
3+
4+
export default function Page(props) {
5+
const router = useRouter()
6+
7+
return (
8+
<>
9+
<p id="gssp">gssp page</p>
10+
<p id="props">{JSON.stringify(props)}</p>
11+
<p id="router-query">{JSON.stringify(router.query)}</p>
12+
<p id="router-pathname">{router.pathname}</p>
13+
<p id="router-as-path">{router.asPath}</p>
14+
<Link href="/">
15+
<a id="to-index">to /</a>
16+
</Link>
17+
<br />
18+
</>
19+
)
20+
}
21+
22+
export const getServerSideProps = ({ query }) => {
23+
if (query.hiding) {
24+
return {
25+
unstable_notFound: true,
26+
}
27+
}
28+
29+
return {
30+
props: {
31+
hello: 'world',
32+
},
33+
}
34+
}

test/integration/getserversideprops/test/index.test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ const expectedManifestRoutes = () => [
118118
),
119119
page: '/non-json',
120120
},
121+
{
122+
dataRouteRegex: `^\\/_next\\/data\\/${escapeRegex(
123+
buildId
124+
)}\\/not-found.json$`,
125+
page: '/not-found',
126+
},
127+
{
128+
dataRouteRegex: `^\\/_next\\/data\\/${escapeRegex(
129+
buildId
130+
)}\\/not\\-found\\/([^\\/]+?)\\.json$`,
131+
namedDataRouteRegex: `^/_next/data/${escapeRegex(
132+
buildId
133+
)}/not\\-found/(?<slug>[^/]+?)\\.json$`,
134+
page: '/not-found/[slug]',
135+
routeKeys: {
136+
slug: 'slug',
137+
},
138+
},
121139
{
122140
dataRouteRegex: normalizeRegEx(
123141
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/refresh.json$`
@@ -235,6 +253,50 @@ const navigateTest = (dev = false) => {
235253
const runTests = (dev = false) => {
236254
navigateTest(dev)
237255

256+
it('should render 404 correctly when notFound is returned (non-dynamic)', async () => {
257+
const res = await fetchViaHTTP(appPort, '/not-found', { hiding: true })
258+
259+
expect(res.status).toBe(404)
260+
expect(await res.text()).toContain('This page could not be found')
261+
})
262+
263+
it('should render 404 correctly when notFound is returned client-transition (non-dynamic)', async () => {
264+
const browser = await webdriver(appPort, '/')
265+
await browser.eval(`(function() {
266+
window.beforeNav = 1
267+
window.next.router.push('/not-found?hiding=true')
268+
})()`)
269+
270+
await browser.waitForElementByCss('h1')
271+
expect(await browser.elementByCss('html').text()).toContain(
272+
'This page could not be found'
273+
)
274+
expect(await browser.eval('window.beforeNav')).toBe(null)
275+
})
276+
277+
it('should render 404 correctly when notFound is returned (dynamic)', async () => {
278+
const res = await fetchViaHTTP(appPort, '/not-found/first', {
279+
hiding: true,
280+
})
281+
282+
expect(res.status).toBe(404)
283+
expect(await res.text()).toContain('This page could not be found')
284+
})
285+
286+
it('should render 404 correctly when notFound is returned client-transition (dynamic)', async () => {
287+
const browser = await webdriver(appPort, '/')
288+
await browser.eval(`(function() {
289+
window.beforeNav = 1
290+
window.next.router.push('/not-found/first?hiding=true')
291+
})()`)
292+
293+
await browser.waitForElementByCss('h1')
294+
expect(await browser.elementByCss('html').text()).toContain(
295+
'This page could not be found'
296+
)
297+
expect(await browser.eval('window.beforeNav')).toBe(null)
298+
})
299+
238300
it('should SSR normal page correctly', async () => {
239301
const html = await renderViaHTTP(appPort, '/')
240302
expect(html).toMatch(/hello.*?world/)

0 commit comments

Comments
 (0)