Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
import { RedirectStatusCode } from '../../client/components/redirect-status-code'
import { DevBundlerService } from './dev-bundler-service'
import { type Span, trace } from '../../trace'
import { ensureLeadingSlash } from '../../shared/lib/page-path/ensure-leading-slash'

const debug = setupDebug('next:router-server:main')
const isNextFont = (pathname: string | null) =>
Expand Down Expand Up @@ -585,13 +586,15 @@ export async function initialize(opts: {
})

if (opts.dev && developmentBundler && req.url) {
const isHMRRequest = req.url.includes('/_next/webpack-hmr')
const { basePath, assetPrefix } = config

const isHMRRequest = req.url.startsWith(
ensureLeadingSlash(`${assetPrefix || basePath}/_next/webpack-hmr`)
)

// only handle HMR requests if the basePath in the request
// matches the basePath for the handler responding to the request
const isRequestForCurrentBasepath =
!config.basePath || pathHasPrefix(req.url, config.basePath)

if (isHMRRequest && isRequestForCurrentBasepath) {
if (isHMRRequest) {
return developmentBundler.hotReloader.onHMR(req, socket, head)
}
}
Expand Down
3 changes: 0 additions & 3 deletions test/e2e/multi-zone/app/apps/first/next.config.js

This file was deleted.

11 changes: 0 additions & 11 deletions test/e2e/multi-zone/app/apps/first/pages/index.tsx

This file was deleted.

19 changes: 0 additions & 19 deletions test/e2e/multi-zone/app/apps/first/tsconfig.json

This file was deleted.

3 changes: 3 additions & 0 deletions test/e2e/multi-zone/app/apps/guest/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
basePath: '/guest',
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.1",
"private": true,
"name": "first-app"
"name": "guest-app"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default function Page() {
return <p>hello from second app /another/[slug]</p>
return <p>hello from guest app /another/[slug]</p>
}

export function getServerSideProps({ params }) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default function Page() {
return <p>hello from first app /blog/[slug]</p>
return <p>hello from guest app /blog/[slug]</p>
}

export function getServerSideProps({ params }) {
Expand Down
19 changes: 19 additions & 0 deletions test/e2e/multi-zone/app/apps/guest/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useEffect, useState } from 'react'

export default function Page() {
const [date, setDate] = useState<Number>()

useEffect(() => {
setDate(Date.now())
}, [])

// this variable is edited by the test to verify HMR
const editedContent = ''
return (
<>
<p>hello from guest app</p>
<div>{editedContent}</div>
<div id="now">{`${date}`}</div>
</>
)
}
1 change: 1 addition & 0 deletions test/e2e/multi-zone/app/apps/host/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.1",
"private": true,
"name": "second-app"
"name": "host-app"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default function Page() {
return <p>hello from second app /blog/[slug]</p>
return <p>hello from host app /blog/[slug]</p>
}

export function getServerSideProps({ params }) {
Expand Down
19 changes: 19 additions & 0 deletions test/e2e/multi-zone/app/apps/host/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useEffect, useState } from 'react'

export default function Page() {
const [date, setDate] = useState<Number>()

useEffect(() => {
setDate(Date.now())
}, [])

// this variable is edited by the test to verify HMR
const editedContent = ''
return (
<>
<p>hello from host app</p>
<div>{editedContent}</div>
<div id="now">{`${date}`}</div>
</>
)
}
3 changes: 0 additions & 3 deletions test/e2e/multi-zone/app/apps/second/next.config.js

This file was deleted.

11 changes: 0 additions & 11 deletions test/e2e/multi-zone/app/apps/second/pages/index.tsx

This file was deleted.

19 changes: 0 additions & 19 deletions test/e2e/multi-zone/app/apps/second/tsconfig.json

This file was deleted.

2 changes: 1 addition & 1 deletion test/e2e/multi-zone/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"dev": "node server.js",
"build": "yarn next build apps/first && yarn next build apps/second",
"build": "yarn next build apps/host && yarn next build apps/guest",
"start": "NODE_ENV=production node server.js"
},
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/multi-zone/app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const http = require('http')
const requestHandlers = new Map()
const dev = process.env.NODE_ENV !== 'production'

for (const appName of ['first', 'second']) {
for (const appName of ['host', 'guest']) {
const appDir = path.join(__dirname, 'apps', appName)
const nextApp = next({
dir: appDir,
Expand All @@ -20,7 +20,7 @@ const http = require('http')
}

const server = http.createServer(async (req, res) => {
const appName = req.url?.split('/')[1].split('?')[0]
const appName = req.url.startsWith('/guest') ? 'guest' : 'host'
const handler = requestHandlers.get(appName)

if (!handler) {
Expand Down
23 changes: 12 additions & 11 deletions test/e2e/multi-zone/multi-zone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ createNextDescribe(
},
({ next, isNextDev }) => {
it.each([
{ pathname: '/first', content: ['hello from first app'] },
{ pathname: '/second', content: ['hello from second app'] },
{ pathname: '/', content: ['hello from host app'] },
{ pathname: '/guest', content: ['hello from guest app'] },
{
pathname: '/first/blog/post-1',
content: ['hello from first app /blog/[slug]'],
pathname: '/blog/post-1',
content: ['hello from host app /blog/[slug]'],
},
{
pathname: '/second/blog/post-1',
content: ['hello from second app /blog/[slug]'],
pathname: '/guest/blog/post-1',
content: ['hello from guest app /blog/[slug]'],
},
{
pathname: '/second/another/post-1',
content: ['hello from second app /another/[slug]'],
pathname: '/guest/another/post-1',
content: ['hello from guest app /another/[slug]'],
},
])(
'should correctly respond for $pathname',
Expand All @@ -49,7 +49,8 @@ createNextDescribe(

if (isNextDev) {
async function runHMRTest(app: string) {
const browser = await next.browser(`/${app}`)
const isHostApp = app === 'host'
const browser = await next.browser(isHostApp ? '/' : app)
expect(await browser.elementByCss('body').text()).toContain(
`hello from ${app} app`
)
Expand Down Expand Up @@ -82,8 +83,8 @@ createNextDescribe(
}

it('should support HMR in both apps', async () => {
await runHMRTest('first')
await runHMRTest('second')
await runHMRTest('host')
await runHMRTest('guest')
})
}
}
Expand Down