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

Ensure id handling applies for dynamic import in pages #51049

Merged
merged 1 commit into from Jun 9, 2023
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
15 changes: 15 additions & 0 deletions packages/next/src/client/index.tsx
Expand Up @@ -63,6 +63,21 @@ declare global {
}
}

// ensure dynamic imports have deployment id added if enabled
const getChunkScriptFilename = __webpack_require__.u
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is u here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's webpack's script URL method, this matches how it's handled in app

__webpack_require__.u = (chunkId: any) => {


// eslint-disable-next-line no-undef
__webpack_require__.u = (chunkId: any) => {
return (
getChunkScriptFilename(chunkId) +
`${
process.env.__NEXT_DEPLOYMENT_ID
? `?dpl=${process.env.__NEXT_DEPLOYMENT_ID}`
: ''
}`
)
}

type RenderRouteInfo = PrivateRouteInfo & {
App: AppComponent
scroll?: { x: number; y: number } | null
Expand Down
@@ -1,3 +1,4 @@
'use client'
import testImage from '../../../public/test.jpg'
import Image from 'next/image'

Expand All @@ -6,6 +7,17 @@ export default function Page() {
<>
<p>hello app edge</p>
<Image src={testImage} alt="test" />

<button
onClick={() => {
import('../../../data').then((mod) => {
console.log('loaded data', mod)
})
}}
id="dynamic-import"
>
click me
</button>
</>
)
}
Expand Down
@@ -1,3 +1,4 @@
'use client'
import testImage from '../../public/test.jpg'
import Image from 'next/image'

Expand All @@ -6,6 +7,17 @@ export default function Page() {
<>
<p>hello app</p>
<Image src={testImage} alt="test" />

<button
onClick={() => {
import('../../data').then((mod) => {
console.log('loaded data', mod)
})
}}
id="dynamic-import"
>
click me
</button>
</>
)
}
3 changes: 3 additions & 0 deletions test/production/deployment-id-handling/app/data.js
@@ -0,0 +1,3 @@
export const data = {
now: Date.now(),
}
11 changes: 11 additions & 0 deletions test/production/deployment-id-handling/app/pages/index.tsx
Expand Up @@ -6,6 +6,17 @@ export default function Page() {
<>
<p>hello pages</p>
<Image src={testImage} alt="test image" />

<button
onClick={() => {
import('../data').then((mod) => {
console.log('loaded data', mod)
})
}}
id="dynamic-import"
>
click me
</button>
</>
)
}
11 changes: 11 additions & 0 deletions test/production/deployment-id-handling/app/pages/pages-edge.tsx
Expand Up @@ -6,6 +6,17 @@ export default function Page() {
<>
<p>hello pages edge</p>
<Image src={testImage} alt="test image" />

<button
onClick={() => {
import('../data').then((mod) => {
console.log('loaded data', mod)
})
}}
id="dynamic-import"
>
click me
</button>
</>
)
}
Expand Down
@@ -1,4 +1,5 @@
import { createNextDescribe } from 'e2e-utils'
import { check } from 'next-test-utils'
import { join } from 'node:path'

const deploymentId = Date.now() + ''
Expand Down Expand Up @@ -44,6 +45,28 @@ createNextDescribe(
expect(link.attribs.href).toContain('dpl=' + deploymentId)
}
}

const browser = await next.browser(urlPath)
const requests = []

browser.on('request', (req) => {
requests.push(req.url())
})

await browser.elementByCss('#dynamic-import').click()

await check(
() => (requests.length > 0 ? 'success' : JSON.stringify(requests)),
'success'
)

try {
expect(
requests.every((item) => item.includes('dpl=' + deploymentId))
).toBe(true)
} finally {
require('console').error('requests', requests)
}
}
)
}
Expand Down