-
Notifications
You must be signed in to change notification settings - Fork 26.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure resolvedUrl is correct with fallback rewrites #37932
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 32 additions & 0 deletions
32
test/production/minimal-mode-response-cache/app/next.config.js
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,32 @@ | ||
module.exports = { | ||
experimental: { | ||
outputStandalone: true, | ||
}, | ||
trailingSlash: true, | ||
rewrites() { | ||
return { | ||
beforeFiles: [ | ||
{ | ||
source: '/news/:path/', | ||
destination: '/news/:path*/', | ||
}, | ||
], | ||
afterFiles: [ | ||
{ | ||
source: '/somewhere', | ||
destination: '/', | ||
}, | ||
], | ||
fallback: [ | ||
{ | ||
source: '/:path*', | ||
destination: '/:path*', | ||
}, | ||
{ | ||
source: '/(.*)', | ||
destination: '/seo-redirects', | ||
}, | ||
], | ||
} | ||
}, | ||
} |
32 changes: 32 additions & 0 deletions
32
test/production/minimal-mode-response-cache/app/pages/blog/[slug].js
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,32 @@ | ||
import { useRouter } from 'next/router' | ||
|
||
export default function Page(props) { | ||
const router = useRouter() | ||
|
||
return ( | ||
<> | ||
<p id="page">/blog/[slug]</p> | ||
<p id="props">{JSON.stringify(props)}</p> | ||
<p id="asPath">{router.asPath}</p> | ||
<p id="pathname">{router.pathname}</p> | ||
<p id="query">{JSON.stringify(router.query)}</p> | ||
</> | ||
) | ||
} | ||
|
||
export function getStaticProps({ params }) { | ||
console.log('getStaticProps /blog/[slug]', params) | ||
return { | ||
props: { | ||
params, | ||
now: Date.now(), | ||
}, | ||
} | ||
} | ||
|
||
export function getStaticPaths() { | ||
return { | ||
paths: ['/blog/first'], | ||
fallback: 'blocking', | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
test/production/minimal-mode-response-cache/app/pages/index.js
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,25 @@ | ||
import { useRouter } from 'next/router' | ||
|
||
export default function Page(props) { | ||
const router = useRouter() | ||
|
||
return ( | ||
<> | ||
<p id="page">/</p> | ||
<p id="props">{JSON.stringify(props)}</p> | ||
<p id="asPath">{router.asPath}</p> | ||
<p id="pathname">{router.pathname}</p> | ||
<p id="query">{JSON.stringify(router.query)}</p> | ||
</> | ||
) | ||
} | ||
|
||
export function getStaticProps() { | ||
console.log('getStaticProps /') | ||
return { | ||
props: { | ||
index: true, | ||
now: Date.now(), | ||
}, | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
test/production/minimal-mode-response-cache/app/pages/news.js
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,25 @@ | ||
import { useRouter } from 'next/router' | ||
|
||
export default function Page(props) { | ||
const router = useRouter() | ||
|
||
return ( | ||
<> | ||
<p id="page">/news</p> | ||
<p id="props">{JSON.stringify(props)}</p> | ||
<p id="asPath">{router.asPath}</p> | ||
<p id="pathname">{router.pathname}</p> | ||
<p id="query">{JSON.stringify(router.query)}</p> | ||
</> | ||
) | ||
} | ||
|
||
export function getStaticProps() { | ||
console.log('getStaticProps /news') | ||
return { | ||
props: { | ||
news: true, | ||
now: Date.now(), | ||
}, | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
test/production/minimal-mode-response-cache/index.test.ts
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,122 @@ | ||
import glob from 'glob' | ||
import fs from 'fs-extra' | ||
import { join } from 'path' | ||
import cheerio from 'cheerio' | ||
import { createNext, FileRef } from 'e2e-utils' | ||
import { NextInstance } from 'test/lib/next-modes/base' | ||
import { | ||
killApp, | ||
findPort, | ||
renderViaHTTP, | ||
initNextServerScript, | ||
} from 'next-test-utils' | ||
|
||
describe('minimal-mode-response-cache', () => { | ||
let next: NextInstance | ||
let server | ||
let appPort | ||
|
||
beforeAll(async () => { | ||
// test build against environment with next support | ||
process.env.NOW_BUILDER = '1' | ||
|
||
next = await createNext({ | ||
files: new FileRef(join(__dirname, 'app')), | ||
}) | ||
await next.stop() | ||
|
||
await fs.move( | ||
join(next.testDir, '.next/standalone'), | ||
join(next.testDir, 'standalone') | ||
) | ||
for (const file of await fs.readdir(next.testDir)) { | ||
if (file !== 'standalone') { | ||
await fs.remove(join(next.testDir, file)) | ||
console.log('removed', file) | ||
} | ||
} | ||
const files = glob.sync('**/*', { | ||
cwd: join(next.testDir, 'standalone/.next/server/pages'), | ||
dot: true, | ||
}) | ||
|
||
for (const file of files) { | ||
if (file.endsWith('.json') || file.endsWith('.html')) { | ||
await fs.remove(join(next.testDir, '.next/server', file)) | ||
} | ||
} | ||
|
||
const testServer = join(next.testDir, 'standalone/server.js') | ||
await fs.writeFile( | ||
testServer, | ||
(await fs.readFile(testServer, 'utf8')) | ||
.replace('console.error(err)', `console.error('top-level', err)`) | ||
.replace('conf:', 'minimalMode: true,conf:') | ||
) | ||
appPort = await findPort() | ||
server = await initNextServerScript( | ||
testServer, | ||
/Listening on/, | ||
{ | ||
...process.env, | ||
PORT: appPort, | ||
}, | ||
undefined, | ||
{ | ||
cwd: next.testDir, | ||
} | ||
) | ||
}) | ||
afterAll(async () => { | ||
await next.destroy() | ||
if (server) await killApp(server) | ||
}) | ||
|
||
it('should have correct responses', async () => { | ||
const html = await renderViaHTTP(appPort, '/') | ||
expect(html.length).toBeTruthy() | ||
|
||
for (const { path, matchedPath, query, asPath, pathname } of [ | ||
{ path: '/', asPath: '/' }, | ||
{ path: '/', matchedPath: '/index', asPath: '/' }, | ||
{ path: '/', matchedPath: '/index/', asPath: '/' }, | ||
{ path: '/', matchedPath: '/', asPath: '/' }, | ||
{ | ||
path: '/news/', | ||
matchedPath: '/news/', | ||
asPath: '/news/', | ||
pathname: '/news', | ||
}, | ||
{ | ||
path: '/news/', | ||
matchedPath: '/news', | ||
asPath: '/news/', | ||
pathname: '/news', | ||
}, | ||
{ | ||
path: '/blog/first/', | ||
matchedPath: '/blog/first/', | ||
pathname: '/blog/[slug]', | ||
asPath: '/blog/first/', | ||
query: { slug: 'first' }, | ||
}, | ||
{ | ||
path: '/blog/second/', | ||
matchedPath: '/blog/[slug]/', | ||
pathname: '/blog/[slug]', | ||
asPath: '/blog/second/', | ||
query: { slug: 'second' }, | ||
}, | ||
]) { | ||
const html = await renderViaHTTP(appPort, path, undefined, { | ||
headers: { | ||
'x-matched-path': matchedPath || path, | ||
}, | ||
}) | ||
const $ = cheerio.load(html) | ||
expect($('#asPath').text()).toBe(asPath) | ||
expect($('#pathname').text()).toBe(pathname || path) | ||
expect(JSON.parse($('#query').text())).toEqual(query || {}) | ||
} | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit pick: You can use jest
.each()
which will print much easier to read error message when the assertions fail.Example usage:
next.js/test/development/middleware-warnings/index.test.ts
Line 23 in b04cdc9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The timing here is important as we also want to ensure a previous cache entry isn't re-used within the 1 second window so didn't break it into separate
it()
checks as that could add some overhead