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

fix(pages-dir): cannot load the same css module with both import and next/dynamic #68396

Draft
wants to merge 39 commits into
base: canary
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
5da02ef
test: add current behavior
devjiwonchoi Aug 1, 2024
a11a69f
fix: include dynamic css to build manifest
devjiwonchoi Aug 1, 2024
bd244f4
Merge branch 'canary' into 08-01-test_add_current_repro
devjiwonchoi Aug 27, 2024
07fdcb8
flatten build manifest
devjiwonchoi Aug 29, 2024
df616db
add dynamic import test
devjiwonchoi Aug 29, 2024
85c2565
infer types
devjiwonchoi Aug 29, 2024
cacf2f9
better explanation comment
devjiwonchoi Aug 29, 2024
4b97959
recursive block
devjiwonchoi Aug 29, 2024
a5bd4e1
test: build-manifest test
devjiwonchoi Aug 29, 2024
0c32a64
ssr false
devjiwonchoi Sep 3, 2024
a1b75f5
remove unnecessary pages check
devjiwonchoi Sep 4, 2024
9d83e8b
test: add green button
devjiwonchoi Sep 4, 2024
e9678f1
fix
devjiwonchoi Sep 4, 2024
5db8bda
test: simplify
devjiwonchoi Sep 5, 2024
fd81f98
Merge branch 'canary' into 08-01-test_add_current_repro
devjiwonchoi Sep 6, 2024
39fac18
[no ci] update test
devjiwonchoi Sep 6, 2024
1c908f3
mini css
devjiwonchoi Sep 6, 2024
7afa1e0
revert build-manifest but types
devjiwonchoi Sep 6, 2024
7a45075
mini css
devjiwonchoi Sep 6, 2024
060c57e
no build-manifest
devjiwonchoi Sep 6, 2024
236c936
clean up mini css
devjiwonchoi Sep 6, 2024
00f241e
remove data-n-p only if is in build manifest
devjiwonchoi Sep 6, 2024
3ba7a56
wip
devjiwonchoi Sep 6, 2024
dff0235
nope, wrong
devjiwonchoi Sep 7, 2024
b9bb0fe
separate dynamic import test
devjiwonchoi Sep 7, 2024
c1e8596
fix
devjiwonchoi Sep 7, 2024
68b0240
Revert "infer types"
devjiwonchoi Sep 7, 2024
2e6a0b8
revert unnecessary change
devjiwonchoi Sep 7, 2024
203019b
test: edge
devjiwonchoi Sep 7, 2024
0f49f5d
pages only
devjiwonchoi Sep 7, 2024
0147004
test
devjiwonchoi Sep 7, 2024
9950f12
add repro for dynamic import
devjiwonchoi Sep 7, 2024
0e8d92e
add route to react-loadable-manifest
devjiwonchoi Sep 8, 2024
faf0123
dynamic css
devjiwonchoi Sep 8, 2024
9f4384c
test
devjiwonchoi Sep 9, 2024
7eeb237
revert
devjiwonchoi Sep 10, 2024
1ad8ba2
revert unnecessary changes
devjiwonchoi Sep 10, 2024
9a41a6b
on demand
devjiwonchoi Sep 12, 2024
e1f6e13
on demand
devjiwonchoi Sep 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions test/e2e/pages-dir/css-module/components/red-button-lazy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useLoadOnClientSide } from './useLoadOnClientSide'
const loader = () => import('./red-button').then(({ RedButton }) => RedButton)

export function RedButtonLazy() {
const RedButton = useLoadOnClientSide(loader, null)
return RedButton && <RedButton />
}
9 changes: 9 additions & 0 deletions test/e2e/pages-dir/css-module/components/red-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import classes from './red.module.css'

export function RedButton() {
return (
<button id="red-button" className={classes.button}>
My background should be red!
</button>
)
}
3 changes: 3 additions & 0 deletions test/e2e/pages-dir/css-module/components/red.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.button {
background-color: rgb(255, 0, 0);
}
26 changes: 26 additions & 0 deletions test/e2e/pages-dir/css-module/components/useLoadOnClientSide.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useEffect, useState } from 'react'

const empty = {}

export const useLoadOnClientSide = (loader, fallback) => {
const [lazyModule, setLazyModule] = useState(empty)
const skip = lazyModule !== empty

useEffect(() => {
if (skip || typeof document === 'undefined') {
return
}

let mounted = true
loader().then((mod) => {
if (!mounted) return
setLazyModule(() => mod)
})

return () => {
mounted = false
}
}, [skip, loader])

return lazyModule === empty ? fallback : lazyModule
}
49 changes: 49 additions & 0 deletions test/e2e/pages-dir/css-module/dynamic-import.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { nextTestSetup } from 'e2e-utils'

describe('pages-dir-css-module-next-dynamic-client-navigation', () => {
const { next } = nextTestSetup({
files: __dirname,
})

describe('nodejs', () => {
it('should not remove style when navigating from static imported component to dynamic import', async () => {
const browser = await next.browser('/dynamic-import/nodejs')
expect(
await browser
.elementByCss('a[href="/dynamic-import/basic"]')
.click()
.waitForElementByCss('#red-button')
.text()
).toBe('My background should be red!')

const buttonBgColor = await browser.eval(
`window.getComputedStyle(document.querySelector('button')).backgroundColor`
)
// not gray
expect(buttonBgColor).not.toBe('rgb(239, 239, 239)')
// but red
expect(buttonBgColor).toBe('rgb(255, 0, 0)')
})
})

describe('edge', () => {
it('should not remove style when navigating from static imported component to dynamic import', async () => {
const browser = await next.browser('/dynamic-import/edge')
expect(
await browser
.elementByCss('a[href="/dynamic-import/basic"]')
.click()
.waitForElementByCss('#red-button')
.text()
).toBe('My background should be red!')

const buttonBgColor = await browser.eval(
`window.getComputedStyle(document.querySelector('button')).backgroundColor`
)
// not gray
expect(buttonBgColor).not.toBe('rgb(239, 239, 239)')
// but red
expect(buttonBgColor).toBe('rgb(255, 0, 0)')
})
})
})
147 changes: 147 additions & 0 deletions test/e2e/pages-dir/css-module/next-dynamic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { nextTestSetup } from 'e2e-utils'

describe('pages-dir-css-module-next-dynamic-client-navigation', () => {
const { next } = nextTestSetup({
files: __dirname,
})

describe('nodejs', () => {
it('should not remove style when navigating from static imported component to next/dynamic', async () => {
const browser = await next.browser('/next-dynamic/nodejs')
expect(
await browser
.elementByCss('a[href="/next-dynamic/basic"]')
.click()
.waitForElementByCss('#red-button')
.text()
).toBe('My background should be red!')

const buttonBgColor = await browser.eval(
`window.getComputedStyle(document.querySelector('button')).backgroundColor`
)
// red
expect(buttonBgColor).toBe('rgb(255, 0, 0)')
})

it('should not remove style when navigating from static imported component to next/dynamic ssr: false', async () => {
const browser = await next.browser('/next-dynamic/nodejs')
expect(
await browser
.elementByCss('a[href="/next-dynamic/ssr-false"]')
.click()
.waitForElementByCss('#red-button')
.text()
).toBe('My background should be red!')

const buttonBgColor = await browser.eval(
`window.getComputedStyle(document.querySelector('button')).backgroundColor`
)
// red
expect(buttonBgColor).toBe('rgb(255, 0, 0)')
})

it('should not remove style when navigating from static imported component to on demand next/dynamic', async () => {
const browser = await next.browser('/next-dynamic/nodejs')
expect(
await browser
.elementByCss('a[href="/next-dynamic/on-demand"]')
.click()
.waitForElementByCss('#red-button')
.text()
).toBe('My background should be red!')

const buttonBgColor = await browser.eval(
`window.getComputedStyle(document.querySelector('button')).backgroundColor`
)
// red
expect(buttonBgColor).toBe('rgb(255, 0, 0)')
})

it('should not remove style when navigating from static imported component with on demand next/dynamic to on demand next/dynamic', async () => {
const browser = await next.browser('/next-dynamic/nodejs/on-demand')
expect(
await browser
.elementByCss('a[href="/next-dynamic/on-demand"]')
.click()
.waitForElementByCss('#red-button')
.text()
).toBe('My background should be red!')

const buttonBgColor = await browser.eval(
`window.getComputedStyle(document.querySelector('button')).backgroundColor`
)
// red
expect(buttonBgColor).toBe('rgb(255, 0, 0)')
})
})

describe('edge', () => {
it('should not remove style when navigating from static imported component to next/dynamic', async () => {
const browser = await next.browser('/next-dynamic/edge')
expect(
await browser
.elementByCss('a[href="/next-dynamic/basic"]')
.click()
.waitForElementByCss('#red-button')
.text()
).toBe('My background should be red!')

const buttonBgColor = await browser.eval(
`window.getComputedStyle(document.querySelector('button')).backgroundColor`
)
// red
expect(buttonBgColor).toBe('rgb(255, 0, 0)')
})

it('should not remove style when navigating from static imported component to next/dynamic ssr: false', async () => {
const browser = await next.browser('/next-dynamic/edge')
expect(
await browser
.elementByCss('a[href="/next-dynamic/ssr-false"]')
.click()
.waitForElementByCss('#red-button')
.text()
).toBe('My background should be red!')

const buttonBgColor = await browser.eval(
`window.getComputedStyle(document.querySelector('button')).backgroundColor`
)
// red
expect(buttonBgColor).toBe('rgb(255, 0, 0)')
})

it('should not remove style when navigating from static imported component to on demand next/dynamic', async () => {
const browser = await next.browser('/next-dynamic/edge')
expect(
await browser
.elementByCss('a[href="/next-dynamic/on-demand"]')
.click()
.waitForElementByCss('#red-button')
.text()
).toBe('My background should be red!')

const buttonBgColor = await browser.eval(
`window.getComputedStyle(document.querySelector('button')).backgroundColor`
)
// red
expect(buttonBgColor).toBe('rgb(255, 0, 0)')
})

it('should not remove style when navigating from static imported component with on demand next/dynamic to on demand next/dynamic', async () => {
const browser = await next.browser('/next-dynamic/edge/on-demand')
expect(
await browser
.elementByCss('a[href="/next-dynamic/on-demand"]')
.click()
.waitForElementByCss('#red-button')
.text()
).toBe('My background should be red!')

const buttonBgColor = await browser.eval(
`window.getComputedStyle(document.querySelector('button')).backgroundColor`
)
// red
expect(buttonBgColor).toBe('rgb(255, 0, 0)')
})
})
})
3 changes: 3 additions & 0 deletions test/e2e/pages-dir/css-module/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
reactStrictMode: true,
}
5 changes: 5 additions & 0 deletions test/e2e/pages-dir/css-module/pages/dynamic-import/basic.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { RedButtonLazy } from '../../components/red-button-lazy'

export default function Home() {
return <RedButtonLazy />
}
18 changes: 18 additions & 0 deletions test/e2e/pages-dir/css-module/pages/dynamic-import/edge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Link from 'next/link'
import { RedButton } from '../../components/red-button'
import { RedButtonLazy } from '../../components/red-button-lazy'

export default function Home() {
return (
<>
<Link href="/dynamic-import/basic">/dynamic-import/basic</Link>
{/* Both RedButton and RedButtonLazy should be imported to reproduce the issue */}
<RedButton />
<RedButtonLazy />
</>
)
}

export const config = {
runtime: 'experimental-edge',
}
14 changes: 14 additions & 0 deletions test/e2e/pages-dir/css-module/pages/dynamic-import/nodejs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Link from 'next/link'
import { RedButton } from '../../components/red-button'
import { RedButtonLazy } from '../../components/red-button-lazy'

export default function Home() {
return (
<>
<Link href="/dynamic-import/basic">/dynamic-import/basic</Link>
{/* Both RedButton and RedButtonLazy should be imported to reproduce the issue */}
<RedButton />
<RedButtonLazy />
</>
)
}
19 changes: 19 additions & 0 deletions test/e2e/pages-dir/css-module/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Link from 'next/link'

export default function Home() {
return (
<div
style={{
display: 'flex',
flexDirection: 'column',
gap: '10px',
width: '400px',
}}
>
<Link href="/next-dynamic/nodejs">/next-dynamic/nodejs</Link>
<Link href="/next-dynamic/edge">/next-dynamic/edge</Link>
<Link href="/dynamic-import/nodejs">/dynamic-import/nodejs</Link>
<Link href="/dynamic-import/edge">/dynamic-import/edge</Link>
</div>
)
}
9 changes: 9 additions & 0 deletions test/e2e/pages-dir/css-module/pages/next-dynamic/basic.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import dynamic from 'next/dynamic'

const NextDynamicRedButton = dynamic(() =>
import('../../components/red-button').then((module) => module.RedButton)
)

export default function NextDynamic() {
return <NextDynamicRedButton />
}
25 changes: 25 additions & 0 deletions test/e2e/pages-dir/css-module/pages/next-dynamic/edge/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Link from 'next/link'
import { RedButton } from '../../../components/red-button'

export default function Home() {
return (
<div
style={{
display: 'flex',
flexDirection: 'column',
gap: '10px',
width: '400px',
}}
>
<Link href="/next-dynamic/basic">/next-dynamic/basic</Link>
<Link href="/next-dynamic/ssr-false">/next-dynamic/ssr-false</Link>
<Link href="/next-dynamic/on-demand">/next-dynamic/on-demand</Link>
{/* RedButton should be imported to reproduce the issue */}
<RedButton />
</div>
)
}

export const config = {
runtime: 'experimental-edge',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Link from 'next/link'
import dynamic from 'next/dynamic'
import { useState } from 'react'
import { RedButton } from '../../../components/red-button'

const NextDynamicRedButton = dynamic(
() =>
import('../../../components/red-button').then((module) => module.RedButton),
{ ssr: false }
)

export default function Home() {
const [button, setButton] = useState(<button>Red Button on Standby</button>)
const handleClick = () => {
setButton(<NextDynamicRedButton />)
}

return (
<div
style={{
display: 'flex',
flexDirection: 'column',
gap: '10px',
width: '400px',
}}
>
<Link href="/next-dynamic/on-demand">/next-dynamic/on-demand</Link>
<button onClick={handleClick}>Click to load red button</button>
<RedButton />
{button}
</div>
)
}

export const config = {
runtime: 'experimental-edge',
}
Loading
Loading