Skip to content

Commit

Permalink
Change tree shaking of resolve-rewrites
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed Mar 19, 2021
1 parent 1100f29 commit f731a59
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 61 deletions.
10 changes: 0 additions & 10 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,6 @@ export default async function getBaseWebpackConfig(
}
}

const clientResolveRewrites = require.resolve(
'next/dist/next-server/lib/router/utils/resolve-rewrites'
)
const clientResolveRewritesNoop = require.resolve(
'next/dist/next-server/lib/router/utils/resolve-rewrites-noop'
)

const resolveConfig = {
// Disable .mjs for node_modules bundling
extensions: isServer
Expand Down Expand Up @@ -384,9 +377,6 @@ export default async function getBaseWebpackConfig(
[DOT_NEXT_ALIAS]: distDir,
...getOptimizedAliases(isServer),
...getReactProfilingInProduction(),
[clientResolveRewrites]: hasRewrites
? clientResolveRewrites
: clientResolveRewritesNoop,
},
...(isWebpack5 && !isServer
? {
Expand Down
106 changes: 55 additions & 51 deletions packages/next/next-server/lib/router/utils/resolve-rewrites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,65 +16,69 @@ export default function resolveRewrites(
query: ParsedUrlQuery,
resolveHref: (path: string) => string,
locales?: string[]
): {
matchedPage: boolean
parsedAs: ReturnType<typeof parseRelativeUrl>
asPath: string
resolvedHref?: string
} {
let matchedPage = false
let parsedAs = parseRelativeUrl(asPath)
let fsPathname = removePathTrailingSlash(
normalizeLocalePath(delBasePath(parsedAs.pathname), locales).pathname
)
let resolvedHref
):
| {
matchedPage: boolean
parsedAs: ReturnType<typeof parseRelativeUrl>
asPath: string
resolvedHref?: string
}
| undefined {
if (process.env.__NEXT_HAS_REWRITES) {
let matchedPage = false
let parsedAs = parseRelativeUrl(asPath)
let fsPathname = removePathTrailingSlash(
normalizeLocalePath(delBasePath(parsedAs.pathname), locales).pathname
)
let resolvedHref

if (!pages.includes(fsPathname)) {
for (const rewrite of rewrites) {
const matcher = customRouteMatcher(rewrite.source)
const params = matcher(parsedAs.pathname)
if (!pages.includes(fsPathname)) {
for (const rewrite of rewrites) {
const matcher = customRouteMatcher(rewrite.source)
const params = matcher(parsedAs.pathname)

if (params) {
if (!rewrite.destination) {
// this is a proxied rewrite which isn't handled on the client
break
}
const destRes = prepareDestination(
rewrite.destination,
params,
query,
true
)
parsedAs = destRes.parsedDestination
asPath = destRes.newUrl
Object.assign(query, destRes.parsedDestination.query)
if (params) {
if (!rewrite.destination) {
// this is a proxied rewrite which isn't handled on the client
break
}
const destRes = prepareDestination(
rewrite.destination,
params,
query,
true
)
parsedAs = destRes.parsedDestination
asPath = destRes.newUrl
Object.assign(query, destRes.parsedDestination.query)

fsPathname = removePathTrailingSlash(
normalizeLocalePath(delBasePath(asPath), locales).pathname
)
fsPathname = removePathTrailingSlash(
normalizeLocalePath(delBasePath(asPath), locales).pathname
)

if (pages.includes(fsPathname)) {
// check if we now match a page as this means we are done
// resolving the rewrites
matchedPage = true
resolvedHref = fsPathname
break
}
if (pages.includes(fsPathname)) {
// check if we now match a page as this means we are done
// resolving the rewrites
matchedPage = true
resolvedHref = fsPathname
break
}

// check if we match a dynamic-route, if so we break the rewrites chain
resolvedHref = resolveHref(fsPathname)
// check if we match a dynamic-route, if so we break the rewrites chain
resolvedHref = resolveHref(fsPathname)

if (resolvedHref !== asPath && pages.includes(resolvedHref)) {
matchedPage = true
break
if (resolvedHref !== asPath && pages.includes(resolvedHref)) {
matchedPage = true
break
}
}
}
}
}
return {
asPath,
parsedAs,
matchedPage,
resolvedHref,
return {
asPath,
parsedAs,
matchedPage,
resolvedHref,
}
}
}

0 comments on commit f731a59

Please sign in to comment.