Skip to content
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

Fix static file check with i18n #33503

Merged
merged 3 commits into from Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -740,7 +740,8 @@ export default abstract class Server {
res,
pathname,
{ ..._parsedUrl.query, _nextDataReq: '1' },
parsedUrl
parsedUrl,
true
)
return {
finished: true,
@@ -962,7 +963,7 @@ export default abstract class Server {
}

try {
await this.render(req, res, pathname, query, parsedUrl)
await this.render(req, res, pathname, query, parsedUrl, true)

return {
finished: true,
@@ -1183,7 +1184,8 @@ export default abstract class Server {
res: BaseNextResponse,
pathname: string,
query: NextParsedUrlQuery = {},
parsedUrl?: NextUrlWithParsedQuery
parsedUrl?: NextUrlWithParsedQuery,
internalRender = false
): Promise<void> {
if (!pathname.startsWith('/')) {
console.warn(
@@ -1206,6 +1208,7 @@ export default abstract class Server {
// we don't modify the URL for _next/data request but still
// call render so we special case this to prevent an infinite loop
if (
!internalRender &&
!this.minimalMode &&
!query._nextDataReq &&
(req.url?.match(/^\/_next\//) ||
@@ -216,7 +216,7 @@ export default class DevServer extends Server {

const mergedQuery = { ...urlQuery, ...query }

await this.render(req, res, page, mergedQuery, parsedUrl)
await this.render(req, res, page, mergedQuery, parsedUrl, true)
return {
finished: true,
}
@@ -601,14 +601,16 @@ export default class NextNodeServer extends BaseServer {
res: BaseNextResponse | ServerResponse,
pathname: string,
query?: NextParsedUrlQuery,
parsedUrl?: NextUrlWithParsedQuery
parsedUrl?: NextUrlWithParsedQuery,
internal = false
): Promise<void> {
return super.render(
this.normalizeReq(req),
this.normalizeRes(res),
pathname,
query,
parsedUrl
parsedUrl,
internal
)
}

@@ -78,6 +78,7 @@ export default class Router {
dynamicRoutes: DynamicRoutes
useFileSystemPublicRoutes: boolean
locales: string[]
seenRequests: Set<any>

constructor({
basePath = '',
@@ -123,6 +124,7 @@ export default class Router {
this.dynamicRoutes = dynamicRoutes
this.useFileSystemPublicRoutes = useFileSystemPublicRoutes
this.locales = locales
this.seenRequests = new Set()
}

setDynamicRoutes(routes: DynamicRoutes = []) {
@@ -138,6 +140,13 @@ export default class Router {
res: BaseNextResponse,
parsedUrl: NextUrlWithParsedQuery
): Promise<boolean> {
if (this.seenRequests.has(req)) {
throw new Error(
`Invariant: request has already been processed: ${req.url}, this is an internal error please open an issue.`
)
}
this.seenRequests.add(req)

// memoize page check calls so we don't duplicate checks for pages
const pageChecks: { [name: string]: Promise<boolean> } = {}
const memoizedPageChecker = async (p: string): Promise<boolean> => {
@@ -360,6 +369,7 @@ export default class Router {
) {
if (requireBasePath) {
// consider this a non-match so the 404 renders
this.seenRequests.delete(req)
return false
}
// page checker occurs before rewrites so we need to continue
@@ -374,6 +384,7 @@ export default class Router {

// The response was handled
if (result.finished) {
this.seenRequests.delete(req)
return true
}

@@ -397,11 +408,13 @@ export default class Router {
// check filesystem
if (testRoute.check === true) {
if (await applyCheckTrue(parsedUrlUpdated)) {
this.seenRequests.delete(req)
return true
}
}
}
}
this.seenRequests.delete(req)
return false
}
}
@@ -1,6 +1,7 @@
/* eslint-env jest */

import url from 'url'
import glob from 'glob'
import fs from 'fs-extra'
import cheerio from 'cheerio'
import { join } from 'path'
@@ -56,6 +57,26 @@ export function runTests(ctx) {
})
}

it('should 404 for locale prefixed static assets correctly', async () => {
const assets = glob.sync('**/*.js', {
cwd: join(ctx.appDir, '.next/static'),
})

for (const locale of locales) {
for (const asset of assets) {
// _next/static asset
const res = await fetchViaHTTP(
ctx.appPort,
`${ctx.basePath || ''}/${locale}/_next/static/${asset}`,
undefined,
{ redirect: 'manual' }
)
expect(res.status).toBe(404)
expect(await res.text()).toContain('could not be found')
}
}
})

it('should redirect external domain correctly', async () => {
const res = await fetchViaHTTP(
ctx.appPort,