Unverified Commit 7c6052a0 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix failing E2E deployment test cases (#36368)

This continues off of https://github.com/vercel/next.js/pull/36285 fixing some of the failing test cases noticed when running the E2E tests against deployments. After these are resolved the tests will be added to our CI flow after each canary release. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

x-ref: https://github.com/vercel/next.js/pull/36285
parent b4d4f241
...@@ -30,7 +30,8 @@ export function getApiHandler(ctx: ServerlessHandlerCtx) { ...@@ -30,7 +30,8 @@ export function getApiHandler(ctx: ServerlessHandlerCtx) {
// We need to trust the dynamic route params from the proxy // We need to trust the dynamic route params from the proxy
// to ensure we are using the correct values // to ensure we are using the correct values
const trustQuery = req.headers[vercelHeader] const trustQuery = req.headers[vercelHeader]
const parsedUrl = handleRewrites(req, parseUrl(req.url!, true)) const parsedUrl = parseUrl(req.url!, true)
handleRewrites(req, parsedUrl)
if (parsedUrl.query.nextInternalLocale) { if (parsedUrl.query.nextInternalLocale) {
delete parsedUrl.query.nextInternalLocale delete parsedUrl.query.nextInternalLocale
......
...@@ -139,7 +139,7 @@ export function getPageHandler(ctx: ServerlessHandlerCtx) { ...@@ -139,7 +139,7 @@ export function getPageHandler(ctx: ServerlessHandlerCtx) {
} }
const origQuery = Object.assign({}, parsedUrl.query) const origQuery = Object.assign({}, parsedUrl.query)
parsedUrl = handleRewrites(req, parsedUrl) handleRewrites(req, parsedUrl)
handleBasePath(req, parsedUrl) handleBasePath(req, parsedUrl)
// remove ?amp=1 from request URL if rendering for export // remove ?amp=1 from request URL if rendering for export
......
...@@ -91,6 +91,8 @@ export function getUtils({ ...@@ -91,6 +91,8 @@ export function getUtils({
req: BaseNextRequest | IncomingMessage, req: BaseNextRequest | IncomingMessage,
parsedUrl: UrlWithParsedQuery parsedUrl: UrlWithParsedQuery
) { ) {
const rewriteParams = {}
for (const rewrite of rewrites) { for (const rewrite of rewrites) {
const matcher = getCustomRouteMatcher(rewrite.source) const matcher = getCustomRouteMatcher(rewrite.source)
let params = matcher(parsedUrl.pathname) let params = matcher(parsedUrl.pathname)
...@@ -106,13 +108,14 @@ export function getUtils({ ...@@ -106,13 +108,14 @@ export function getUtils({
} }
if (params) { if (params) {
const { parsedDestination } = prepareDestination({ const { parsedDestination, destQuery } = prepareDestination({
appendParamsToQuery: true, appendParamsToQuery: true,
destination: rewrite.destination, destination: rewrite.destination,
params: params, params: params,
query: parsedUrl.query, query: parsedUrl.query,
}) })
Object.assign(rewriteParams, destQuery, params)
Object.assign(parsedUrl.query, parsedDestination.query) Object.assign(parsedUrl.query, parsedDestination.query)
delete (parsedDestination as any).query delete (parsedDestination as any).query
...@@ -152,7 +155,7 @@ export function getUtils({ ...@@ -152,7 +155,7 @@ export function getUtils({
} }
} }
return parsedUrl return rewriteParams
} }
function handleBasePath( function handleBasePath(
...@@ -285,7 +288,8 @@ export function getUtils({ ...@@ -285,7 +288,8 @@ export function getUtils({
function normalizeVercelUrl( function normalizeVercelUrl(
req: BaseNextRequest | IncomingMessage, req: BaseNextRequest | IncomingMessage,
trustQuery: boolean trustQuery: boolean,
paramKeys?: string[]
) { ) {
// make sure to normalize req.url on Vercel to strip dynamic params // make sure to normalize req.url on Vercel to strip dynamic params
// from the query which are added during routing // from the query which are added during routing
...@@ -293,7 +297,7 @@ export function getUtils({ ...@@ -293,7 +297,7 @@ export function getUtils({
const _parsedUrl = parseUrl(req.url!, true) const _parsedUrl = parseUrl(req.url!, true)
delete (_parsedUrl as any).search delete (_parsedUrl as any).search
for (const param of Object.keys(defaultRouteRegex.groups)) { for (const param of paramKeys || Object.keys(defaultRouteRegex.groups)) {
delete _parsedUrl.query[param] delete _parsedUrl.query[param]
} }
req.url = formatUrl(_parsedUrl) req.url = formatUrl(_parsedUrl)
......
...@@ -440,21 +440,26 @@ export default abstract class Server { ...@@ -440,21 +440,26 @@ export default abstract class Server {
typeof req.headers['x-matched-path'] === 'string' typeof req.headers['x-matched-path'] === 'string'
) { ) {
const reqUrlIsDataUrl = req.url?.includes('/_next/data') const reqUrlIsDataUrl = req.url?.includes('/_next/data')
const parsedMatchedPath = parseUrl(req.headers['x-matched-path'] || '')
const matchedPathIsDataUrl = const matchedPathIsDataUrl =
req.headers['x-matched-path']?.includes('/_next/data') parsedMatchedPath.pathname?.includes('/_next/data')
const isDataUrl = reqUrlIsDataUrl || matchedPathIsDataUrl const isDataUrl = reqUrlIsDataUrl || matchedPathIsDataUrl
let parsedPath = parseUrl( let parsedPath = parseUrl(
isDataUrl ? req.url! : (req.headers['x-matched-path'] as string), isDataUrl ? req.url! : (req.headers['x-matched-path'] as string),
true true
) )
let matchedPathname = parsedPath.pathname! let matchedPathname = parsedPath.pathname!
let matchedPathnameNoExt = isDataUrl let matchedPathnameNoExt = isDataUrl
? matchedPathname.replace(/\.json$/, '') ? matchedPathname.replace(/\.json$/, '')
: matchedPathname : matchedPathname
let srcPathname = isDataUrl
? parsedMatchedPath.pathname?.replace(/\.json$/, '') ||
matchedPathnameNoExt
: matchedPathnameNoExt
if (this.nextConfig.i18n) { if (this.nextConfig.i18n) {
const localePathResult = normalizeLocalePath( const localePathResult = normalizeLocalePath(
matchedPathname || '/', matchedPathname || '/',
...@@ -469,9 +474,10 @@ export default abstract class Server { ...@@ -469,9 +474,10 @@ export default abstract class Server {
if (isDataUrl) { if (isDataUrl) {
matchedPathname = denormalizePagePath(matchedPathname) matchedPathname = denormalizePagePath(matchedPathname)
matchedPathnameNoExt = denormalizePagePath(matchedPathnameNoExt) matchedPathnameNoExt = denormalizePagePath(matchedPathnameNoExt)
srcPathname = denormalizePagePath(srcPathname)
} }
const pageIsDynamic = isDynamicRoute(matchedPathnameNoExt) const pageIsDynamic = isDynamicRoute(srcPathname)
const combinedRewrites: Rewrite[] = [] const combinedRewrites: Rewrite[] = []
combinedRewrites.push(...this.customRoutes.rewrites.beforeFiles) combinedRewrites.push(...this.customRoutes.rewrites.beforeFiles)
...@@ -480,7 +486,7 @@ export default abstract class Server { ...@@ -480,7 +486,7 @@ export default abstract class Server {
const utils = getUtils({ const utils = getUtils({
pageIsDynamic, pageIsDynamic,
page: matchedPathnameNoExt, page: srcPathname,
i18n: this.nextConfig.i18n, i18n: this.nextConfig.i18n,
basePath: this.nextConfig.basePath, basePath: this.nextConfig.basePath,
rewrites: combinedRewrites, rewrites: combinedRewrites,
...@@ -492,7 +498,15 @@ export default abstract class Server { ...@@ -492,7 +498,15 @@ export default abstract class Server {
if (this.nextConfig.i18n && !url.locale?.path.detectedLocale) { if (this.nextConfig.i18n && !url.locale?.path.detectedLocale) {
parsedUrl.pathname = `/${url.locale?.locale}${parsedUrl.pathname}` parsedUrl.pathname = `/${url.locale?.locale}${parsedUrl.pathname}`
} }
utils.handleRewrites(req, parsedUrl) const pathnameBeforeRewrite = parsedUrl.pathname
const rewriteParams = utils.handleRewrites(req, parsedUrl)
const rewriteParamKeys = Object.keys(rewriteParams)
const didRewrite = pathnameBeforeRewrite !== parsedUrl.pathname
if (didRewrite) {
addRequestMeta(req, '_nextRewroteUrl', parsedUrl.pathname!)
addRequestMeta(req, '_nextDidRewrite', true)
}
// interpolate dynamic params and normalize URL if needed // interpolate dynamic params and normalize URL if needed
if (pageIsDynamic) { if (pageIsDynamic) {
...@@ -538,9 +552,14 @@ export default abstract class Server { ...@@ -538,9 +552,14 @@ export default abstract class Server {
pathname: matchedPathname, pathname: matchedPathname,
}) })
} }
Object.assign(parsedUrl.query, params) Object.assign(parsedUrl.query, params)
utils.normalizeVercelUrl(req, true) }
if (pageIsDynamic || didRewrite) {
utils.normalizeVercelUrl(req, true, [
...rewriteParamKeys,
...Object.keys(utils.defaultRouteRegex?.groups || {}),
])
} }
} catch (err) { } catch (err) {
if (err instanceof DecodeError) { if (err instanceof DecodeError) {
...@@ -1368,6 +1387,14 @@ export default abstract class Server { ...@@ -1368,6 +1387,14 @@ export default abstract class Server {
isRedirect = renderResult.renderOpts.isRedirect isRedirect = renderResult.renderOpts.isRedirect
} else { } else {
const origQuery = parseUrl(req.url || '', true).query const origQuery = parseUrl(req.url || '', true).query
// clear any dynamic route params so they aren't in
// the resolvedUrl
if (opts.params) {
Object.keys(opts.params).forEach((key) => {
delete origQuery[key]
})
}
const hadTrailingSlash = const hadTrailingSlash =
urlPathname !== '/' && this.nextConfig.trailingSlash urlPathname !== '/' && this.nextConfig.trailingSlash
......
...@@ -217,6 +217,7 @@ export function prepareDestination(args: { ...@@ -217,6 +217,7 @@ export function prepareDestination(args: {
return { return {
newUrl, newUrl,
destQuery,
parsedDestination, parsedDestination,
} }
} }
......
...@@ -844,7 +844,12 @@ describe('Prerender', () => { ...@@ -844,7 +844,12 @@ describe('Prerender', () => {
it('should 404 for an invalid data url', async () => { it('should 404 for an invalid data url', async () => {
const res = await fetchViaHTTP(next.url, `/_next/data/${next.buildId}`) const res = await fetchViaHTTP(next.url, `/_next/data/${next.buildId}`)
expect(res.status).toBe(404)
// when deployed this will match due to `index.json` matching the
// directory itself
if (!isDeploy) {
expect(res.status).toBe(404)
}
}) })
it('should allow rewriting to SSG page with fallback: false', async () => { it('should allow rewriting to SSG page with fallback: false', async () => {
......
...@@ -54,6 +54,10 @@ describe('should set-up next', () => { ...@@ -54,6 +54,10 @@ describe('should set-up next', () => {
source: '/some-catch-all/:path*', source: '/some-catch-all/:path*',
destination: '/', destination: '/',
}, },
{
source: '/to-dynamic/post-2',
destination: '/dynamic/post-2?hello=world',
},
{ {
source: '/to-dynamic/:path', source: '/to-dynamic/:path',
destination: '/dynamic/:path', destination: '/dynamic/:path',
...@@ -667,6 +671,44 @@ describe('should set-up next', () => { ...@@ -667,6 +671,44 @@ describe('should set-up next', () => {
expect(await res.text()).toContain('Bad Request') expect(await res.text()).toContain('Bad Request')
}) })
it('should have correct resolvedUrl from rewrite', async () => {
const res = await fetchViaHTTP(appPort, '/to-dynamic/post-1', undefined, {
headers: {
'x-matched-path': '/dynamic/[slug]',
},
})
expect(res.status).toBe(200)
const $ = cheerio.load(await res.text())
expect($('#resolved-url').text()).toBe('/dynamic/post-1')
})
it('should have correct resolvedUrl from rewrite with added query', async () => {
const res = await fetchViaHTTP(appPort, '/to-dynamic/post-2', undefined, {
headers: {
'x-matched-path': '/dynamic/[slug]',
},
})
expect(res.status).toBe(200)
const $ = cheerio.load(await res.text())
expect($('#resolved-url').text()).toBe('/dynamic/post-2')
})
it('should have correct resolvedUrl from dynamic route', async () => {
const res = await fetchViaHTTP(
appPort,
`/_next/data/${next.buildId}/dynamic/post-2.json`,
{ slug: 'post-2' },
{
headers: {
'x-matched-path': '/dynamic/[slug]',
},
}
)
expect(res.status).toBe(200)
const json = await res.json()
expect(json.pageProps.resolvedUrl).toBe('/dynamic/post-2')
})
it('should bubble error correctly for gip page', async () => { it('should bubble error correctly for gip page', async () => {
errors = [] errors = []
const res = await fetchViaHTTP(appPort, '/errors/gip', { crash: '1' }) const res = await fetchViaHTTP(appPort, '/errors/gip', { crash: '1' })
......
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
export const getServerSideProps = ({ params }) => { export const getServerSideProps = ({ params, resolvedUrl }) => {
return { return {
props: { props: {
resolvedUrl,
hello: 'world', hello: 'world',
slug: params.slug, slug: params.slug,
random: Math.random(), random: Math.random(),
...@@ -16,6 +17,7 @@ export default function Page(props) { ...@@ -16,6 +17,7 @@ export default function Page(props) {
<> <>
<p id="dynamic">dynamic page</p> <p id="dynamic">dynamic page</p>
<p id="slug">{props.slug}</p> <p id="slug">{props.slug}</p>
<p id="resolved-url">{props.resolvedUrl}</p>
<p id="router">{JSON.stringify(router)}</p> <p id="router">{JSON.stringify(router)}</p>
<p id="props">{JSON.stringify(props)}</p> <p id="props">{JSON.stringify(props)}</p>
</> </>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment