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

Update to latest watchpack with dynamic route rename fix #10351

Merged
merged 8 commits into from Feb 19, 2020
29 changes: 14 additions & 15 deletions packages/next/server/next-dev-server.ts
Expand Up @@ -28,6 +28,7 @@ import ErrorDebug from './error-debug'
import HotReloader from './hot-reloader'
import { findPageFile } from './lib/find-page-file'
import checkCustomRoutes from '../lib/check-custom-routes'
import { recursiveReadDir } from '../lib/recursive-readdir'

if (typeof React.Suspense === 'undefined') {
throw new Error(
Expand Down Expand Up @@ -157,24 +158,22 @@ export default class DevServer extends Server {
let wp = (this.webpackWatcher = new Watchpack())
wp.watch([], [pagesDir!], 0)

wp.on('aggregated', () => {
wp.on('aggregated', async () => {
const dynamicRoutedPages = []
const knownFiles = wp.getTimeInfoEntries()
for (const [fileName, { accuracy }] of knownFiles) {
if (accuracy === undefined) {
continue
}

let pageName =
'/' + relative(pagesDir!, fileName).replace(/\\+/g, '/')

pageName = pageName.replace(
new RegExp(`\\.+(?:${this.nextConfig.pageExtensions.join('|')})$`),
''
const pageExtensionsRegex = new RegExp(
`\\.+(?:${this.nextConfig.pageExtensions.join('|')})$`
)
const curPageFiles = new Set(
(await recursiveReadDir(this.pagesDir!, pageExtensionsRegex)).map(
f =>
f
.replace(/\\/g, '/')
.replace(pageExtensionsRegex, '')
.replace(/\/index$/, '') || '/'
)
)

pageName = pageName.replace(/\/index$/, '') || '/'

for (const pageName of curPageFiles) {
if (!isDynamicRoute(pageName)) {
continue
}
Expand Down
1 change: 1 addition & 0 deletions test/integration/dynamic-route-rename/pages/[pid].js
@@ -0,0 +1 @@
export default () => 'hi'
53 changes: 53 additions & 0 deletions test/integration/dynamic-route-rename/test/index.test.js
@@ -0,0 +1,53 @@
/* eslint-env jest */
/* global jasmine */
import fs from 'fs-extra'
import { join } from 'path'
import {
renderViaHTTP,
launchApp,
findPort,
killApp,
waitFor,
} from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 30 * 2

let app
let appPort
let stderr = ''

const appDir = join(__dirname, '../')
const pageFile = join(appDir, 'pages/[pid].js')
const pageFileAlt = join(appDir, 'pages/[PiD].js')

describe('Dynamic route rename casing', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort, {
onStderr(msg) {
stderr += msg || ''
},
})
})
afterAll(() => killApp(app))

it('should not throw error when changing casing of dynamic route file', async () => {
// make sure route is loaded in webpack
const html = await renderViaHTTP(appPort, '/abc')
expect(html).toContain('hi')

await fs.rename(pageFile, pageFileAlt)
await waitFor(2000)

expect(stderr).not.toContain(
`You cannot use different slug names for the same dynamic path`
)

await fs.rename(pageFileAlt, pageFile)
await waitFor(2000)

expect(stderr).not.toContain(
`You cannot use different slug names for the same dynamic path`
)
})
})