Skip to content

Commit

Permalink
fix(turbopack): build all parallel routes to make sure HMR works
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Jun 26, 2024
1 parent c8e9e65 commit 168afe8
Showing 1 changed file with 57 additions and 43 deletions.
100 changes: 57 additions & 43 deletions packages/next/src/server/dev/hot-reloader-turbopack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import {
} from './turbopack/entry-key'
import { FAST_REFRESH_RUNTIME_RELOAD } from './messages'
import { generateEncryptionKeyBase64 } from '../app-render/encryption-utils'
import { isAppPageRouteDefinition } from '../route-definitions/app-page-route-definition'

const wsServer = new ws.Server({ noServer: true })
const isTestMode = !!(
Expand Down Expand Up @@ -759,7 +760,7 @@ export async function createHotReloaderTurbopack(
page: inputPage,
// Unused parameters
// clientOnly,
// appPaths,
appPaths,
definition,
isApp,
url: requestUrl,
Expand All @@ -781,6 +782,14 @@ export async function createHotReloaderTurbopack(
const page = routeDef.page
const pathname = definition?.pathname ?? inputPage

let pages = appPaths ?? [page]

// If the route is actually an app page route, then we should have access
// to the app route definition, and therefore, the appPaths from it.
if (!appPaths && definition && isAppPageRouteDefinition(definition)) {
pages = definition.appPaths
}

if (page === '/_error') {
let finishBuilding = startBuilding(pathname, requestUrl, false)
try {
Expand Down Expand Up @@ -808,54 +817,59 @@ export async function createHotReloaderTurbopack(
await currentEntriesHandling

const isInsideAppDir = routeDef.bundlePath.startsWith('app/')
const normalizedAppPage = normalizedPageToTurbopackStructureRoute(
page,
extname(routeDef.filename)
)

const route = isInsideAppDir
? currentEntrypoints.app.get(normalizedAppPage)
: currentEntrypoints.page.get(page)

if (!route) {
// TODO: why is this entry missing in turbopack?
if (page === '/middleware') return
if (page === '/src/middleware') return
if (page === '/instrumentation') return
if (page === '/src/instrumentation') return
const finishBuilding = startBuilding(pathname, requestUrl, false)
try {
// we need to build all parallel routes, so we loop over them here

throw new PageNotFoundError(`route not found ${page}`)
}
/* eslint-disable-next-line @typescript-eslint/no-shadow -- intentionally shadowed*/
for (const page of pages) {
const normalizedAppPage = normalizedPageToTurbopackStructureRoute(
page,
extname(routeDef.filename)
)
const route = isInsideAppDir
? currentEntrypoints.app.get(normalizedAppPage)
: currentEntrypoints.page.get(page)

if (!route) {
// TODO: why is this entry missing in turbopack?
if (page === '/middleware') return
if (page === '/src/middleware') return
if (page === '/instrumentation') return
if (page === '/src/instrumentation') return

throw new PageNotFoundError(`route not found ${page}`)
}

// We don't throw on ensureOpts.isApp === true for page-api
// since this can happen when app pages make
// api requests to page API routes.
if (isApp && route.type === 'page') {
throw new Error(`mis-matched route type: isApp && page for ${page}`)
}
// We don't throw on ensureOpts.isApp === true for page-api
// since this can happen when app pages make
// api requests to page API routes.
if (isApp && route.type === 'page') {
throw new Error(`mis-matched route type: isApp && page for ${page}`)
}

const finishBuilding = startBuilding(pathname, requestUrl, false)
try {
await handleRouteType({
dev: true,
page,
pathname,
route,
currentEntryIssues,
entrypoints: currentEntrypoints,
manifestLoader,
readyIds,
rewrites: opts.fsChecker.rewrites,
logErrors: true,
await handleRouteType({
dev: true,
page,
pathname,
route,
currentEntryIssues,
entrypoints: currentEntrypoints,
manifestLoader,
readyIds,
rewrites: opts.fsChecker.rewrites,
logErrors: true,

hooks: {
subscribeToChanges,
handleWrittenEndpoint: (id, result) => {
clearRequireCache(id, result)
assetMapper.setPathsForKey(id, result.clientPaths)
hooks: {
subscribeToChanges,
handleWrittenEndpoint: (id, result) => {
clearRequireCache(id, result)
assetMapper.setPathsForKey(id, result.clientPaths)
},
},
},
})
})
}
} finally {
finishBuilding()
}
Expand Down

0 comments on commit 168afe8

Please sign in to comment.