forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correct initial fallback route param values (vercel#16485)
This fixes invalid initial route params in development mode and serverless production mode. It also adds tests to ensure these values are correct in development, production, and serverless mode. x-ref: vercel#16084 Fixes: vercel#16481 Fixes: vercel#16482
- Loading branch information
Showing
4 changed files
with
125 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useRouter } from 'next/router' | ||
|
||
export const getStaticProps = () => { | ||
return { | ||
props: { | ||
world: 'world', | ||
}, | ||
} | ||
} | ||
|
||
export const getStaticPaths = () => { | ||
return { | ||
paths: [], | ||
fallback: true, | ||
} | ||
} | ||
|
||
export default function Page({ world }) { | ||
const router = useRouter() | ||
|
||
if (typeof window !== 'undefined' && !window.setInitialSlug) { | ||
window.setInitialSlug = true | ||
window.initialSlug = router.query.slug | ||
} | ||
|
||
if (router.isFallback) return 'Loading...' | ||
|
||
return ( | ||
<> | ||
<p>hello {world}</p> | ||
<p id="query">{JSON.stringify(router.query)}</p> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* eslint-env jest */ | ||
|
||
import fs from 'fs-extra' | ||
import { join } from 'path' | ||
import cheerio from 'cheerio' | ||
import webdriver from 'next-webdriver' | ||
import { | ||
killApp, | ||
findPort, | ||
nextBuild, | ||
nextStart, | ||
renderViaHTTP, | ||
launchApp, | ||
File, | ||
} from 'next-test-utils' | ||
|
||
jest.setTimeout(1000 * 60 * 2) | ||
|
||
const appDir = join(__dirname, '../') | ||
const nextConfig = new File(join(appDir, 'next.config.js')) | ||
let appPort | ||
let app | ||
|
||
const runTests = () => { | ||
it('should have correct fallback query (skeleton)', async () => { | ||
const html = await renderViaHTTP(appPort, '/first') | ||
const $ = cheerio.load(html) | ||
const { query } = JSON.parse($('#__NEXT_DATA__').text()) | ||
expect(query).toEqual({}) | ||
}) | ||
|
||
it('should have correct fallback query (hydration)', async () => { | ||
const browser = await webdriver(appPort, '/second') | ||
const initialSlug = await browser.eval(() => window.initialSlug) | ||
expect(initialSlug).toBe(null) | ||
|
||
await browser.waitForElementByCss('#query') | ||
|
||
const hydratedQuery = JSON.parse( | ||
await browser.elementByCss('#query').text() | ||
) | ||
expect(hydratedQuery).toEqual({ slug: 'second' }) | ||
}) | ||
} | ||
|
||
describe('Fallback Dynamic Route Params', () => { | ||
describe('dev mode', () => { | ||
beforeAll(async () => { | ||
await fs.remove(join(appDir, '.next')) | ||
appPort = await findPort() | ||
app = await launchApp(appDir, appPort) | ||
}) | ||
afterAll(() => killApp(app)) | ||
|
||
runTests() | ||
}) | ||
|
||
describe('production mode', () => { | ||
beforeAll(async () => { | ||
await fs.remove(join(appDir, '.next')) | ||
await nextBuild(appDir, []) | ||
appPort = await findPort() | ||
app = await nextStart(appDir, appPort) | ||
}) | ||
afterAll(() => killApp(app)) | ||
|
||
runTests() | ||
}) | ||
|
||
describe('serverless mode', () => { | ||
beforeAll(async () => { | ||
nextConfig.write(` | ||
module.exports = { | ||
target: 'experimental-serverless-trace' | ||
} | ||
`) | ||
await fs.remove(join(appDir, '.next')) | ||
await nextBuild(appDir, [], { stdout: true }) | ||
appPort = await findPort() | ||
app = await nextStart(appDir, appPort) | ||
}) | ||
afterAll(async () => { | ||
await killApp(app) | ||
nextConfig.delete() | ||
}) | ||
|
||
runTests() | ||
}) | ||
}) |