Skip to content

Commit

Permalink
Fix static file check with i18n (vercel#33503)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk authored and natew committed Feb 16, 2022
1 parent a2037a6 commit d623c6c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
9 changes: 6 additions & 3 deletions packages/next/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,8 @@ export default abstract class Server {
res,
pathname,
{ ..._parsedUrl.query, _nextDataReq: '1' },
parsedUrl
parsedUrl,
true
)
return {
finished: true,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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\//) ||
Expand Down
2 changes: 1 addition & 1 deletion packages/next/server/dev/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
6 changes: 4 additions & 2 deletions packages/next/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down
13 changes: 13 additions & 0 deletions packages/next/server/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default class Router {
dynamicRoutes: DynamicRoutes
useFileSystemPublicRoutes: boolean
locales: string[]
seenRequests: Set<any>

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

setDynamicRoutes(routes: DynamicRoutes = []) {
Expand All @@ -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> => {
Expand Down Expand Up @@ -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
Expand All @@ -374,6 +384,7 @@ export default class Router {

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

Expand All @@ -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
}
}
21 changes: 21 additions & 0 deletions test/integration/i18n-support/test/shared.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d623c6c

Please sign in to comment.