Fix duplicate link type asset generation (#46421)

Addresses https://github.com/vercel/next.js/pull/46378#discussion_r1117924580. During development we collect pages directly from the dev server's watcher, which tracks pages from both node and edge runtimes. Hence here we can ignore the edge server when generating the asset. For build, pages are instead collected from compilation's modules so we need to separate the logic to run in both runtimes' compiler.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
This commit is contained in:
Shu Ding 2023-02-25 20:47:00 +01:00 committed by GitHub
parent 624232d064
commit d9776ccc17
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -122,24 +122,22 @@ async function collectNamedSlots(layoutPath: string) {
return slots
}
let edgeRouteTypes = new Set<string>()
let nodeRouteTypes = new Set<string>()
const edgeRouteTypes: string[] = []
const nodeRouteTypes: string[] = []
export const pageFiles = new Set<string>()
function createRouteDefinitions() {
const fallback = !edgeRouteTypes.size && !nodeRouteTypes.size ? 'string' : ''
const fallback =
!edgeRouteTypes.length && !nodeRouteTypes.length ? 'string' : ''
let routeTypes = ''
edgeRouteTypes.forEach((route) => {
routeTypes += ` | ${route}\n`
})
nodeRouteTypes.forEach((route) => {
if (!edgeRouteTypes.has(route)) {
routeTypes += ` | ${route}\n`
}
routeTypes += ` | ${route}\n`
})
return `declare module 'next' {
@ -271,7 +269,7 @@ export class NextTypesPlugin {
.join('/')
}
;(this.isEdgeServer ? edgeRouteTypes : nodeRouteTypes).add(
;(this.isEdgeServer ? edgeRouteTypes : nodeRouteTypes).push(
`\`${route}\${Suffix}\``
)
}
@ -358,9 +356,9 @@ export class NextTypesPlugin {
// Clear routes
if (this.isEdgeServer) {
edgeRouteTypes = new Set<string>()
edgeRouteTypes.length = 0
} else {
nodeRouteTypes = new Set<string>()
nodeRouteTypes.length = 0
}
compilation.chunkGroups.forEach((chunkGroup: any) => {
@ -396,9 +394,11 @@ export class NextTypesPlugin {
await Promise.all(promises)
if (this.typedRoutes) {
pageFiles.forEach((file) => {
this.collectPage(file)
})
if (this.dev && !this.isEdgeServer) {
pageFiles.forEach((file) => {
this.collectPage(file)
})
}
const linkTypePath = path.join('types', 'link.d.ts')
const assetPath =