Commit 1c5680e9 authored by Will Binns-Smith's avatar Will Binns-Smith

wip

parent 62393b04
use anyhow::Result;
use anyhow::{Context, Result};
use next_core::{
next_edge::entry::wrap_edge_entry,
next_manifests::{InstrumentationDefinition, MiddlewaresManifestV2},
......@@ -213,13 +213,30 @@ impl Endpoint for InstrumentationEndpoint {
vec![]
};
Ok(EndpointOutput {
output_assets: output_assets.to_resolved().await?,
output_paths: EndpointOutputPaths::Edge {
let output_paths = if !this.is_edge {
let node_root = this.project.node_root().owned().await?;
let chunk = self.node_chunk();
let server_entry_path = node_root
.get_path_to(&*chunk.path().await?)
.context(
"Node.js instrumentation chunk entry path must be inside the node root",
)?
.into();
EndpointOutputPaths::NodeJs {
server_entry_path,
server_paths,
client_paths: vec![],
}
.resolved_cell(),
} else {
EndpointOutputPaths::Edge {
server_paths,
client_paths: vec![],
}
};
Ok(EndpointOutput {
output_assets: output_assets.to_resolved().await?,
output_paths: output_paths.resolved_cell(),
project: this.project,
}
.cell())
......
......@@ -359,12 +359,29 @@ impl Endpoint for MiddlewareEndpoint {
(vec![], vec![])
};
Ok(EndpointOutput {
output_paths: EndpointOutputPaths::Edge {
let output_paths = if matches!(this.runtime, NextRuntime::NodeJs) {
let node_root = this.project.node_root().owned().await?;
let chunk = self.node_chunk();
let server_entry_path = node_root
.get_path_to(&*chunk.path().await?)
.context(
"Node.js middleware chunk entry path must be inside the node root",
)?
.into();
EndpointOutputPaths::NodeJs {
server_entry_path,
server_paths,
client_paths,
}
.resolved_cell(),
} else {
EndpointOutputPaths::Edge {
server_paths,
client_paths,
}
};
Ok(EndpointOutput {
output_paths: output_paths.resolved_cell(),
output_assets: output_assets.to_resolved().await?,
project: this.project,
}
......
......@@ -602,15 +602,18 @@ export async function createHotReloaderTurbopack(
join(distDir, p)
)
const { type: entryType } = splitEntryKey(key)
const { type: entryType, page: entryPage } = splitEntryKey(key)
// Server HMR applies to all App Router entries built with the Turbopack
// Node.js runtime: both app pages and route handlers. Edge routes,
// Pages Router pages, and middleware/instrumentation do not use the
// Turbopack Node.js dev runtime and are excluded.
// Server HMR applies to App Router entries, Node.js middleware, and
// Node.js instrumentation built with the Turbopack Node.js runtime. Edge
// routes, Pages Router pages, and edge middleware/instrumentation do not
// use the Turbopack Node.js dev runtime and are excluded.
const usesServerHmr =
serverFastRefresh &&
entryType === 'app' &&
(entryType === 'app' ||
(entryType === 'root' &&
(entryPage === 'middleware' ||
entryPage === 'instrumentation.nodeJs'))) &&
writtenEndpoint.type !== 'edge'
const filesToDelete: string[] = []
......
export function GET() {
return Response.json({
version: (globalThis as any).__instrumentationVersion ?? 'not-set',
depEvaluatedAt:
(globalThis as any).__instrumentationDepEvaluatedAt ?? 'not-set',
})
}
export const _hmrTrigger = 0
export const instrumentationDepEvaluatedAt = Date.now()
import { instrumentationDepEvaluatedAt } from './instrumentation-dep'
// Set at module scope so server HMR re-evaluation updates these globals
;(globalThis as any).__instrumentationVersion = 'v0'
;(globalThis as any).__instrumentationDepEvaluatedAt = instrumentationDepEvaluatedAt
export function register() {}
export const _hmrTrigger = 0
export const depEvaluatedAt = Date.now()
export const runtime = 'nodejs'
import { NextResponse } from 'next/server'
import { depEvaluatedAt } from './middleware-dep'
export default function middleware() {
const res = NextResponse.next()
res.headers.set('x-middleware-version', '0')
res.headers.set('x-middleware-dep-evaluated-at', String(depEvaluatedAt))
return res
}
......@@ -155,6 +155,102 @@ describe('server-hmr', () => {
)
})
describe('nodejs middleware hmr', () => {
it('reflects Node.js middleware changes on next request', async () => {
const initial = await next.fetch('/api/hello')
expect(initial.headers.get('x-middleware-version')).toBe('0')
await next.patchFile('middleware.ts', (content) =>
content.replace("'0'", "'1'")
)
await retry(async () => {
const updated = await next.fetch('/api/hello')
expect(updated.headers.get('x-middleware-version')).toBe('1')
})
})
itTurbopackDev(
'does not re-evaluate unmodified dependency when middleware changes',
async () => {
// Read current version (may have been changed by a previous test)
const initial = await next.fetch('/api/hello')
const initialVersion = initial.headers.get('x-middleware-version')
const initialDepEvaluatedAt = initial.headers.get(
'x-middleware-dep-evaluated-at'
)
expect(initialDepEvaluatedAt).toMatch(/^\d+$/)
// Change only the middleware, not the dependency
await next.patchFile('middleware.ts', (content) =>
content.replace(`'${initialVersion}'`, `'${initialVersion}-updated'`)
)
await retry(async () => {
const updated = await next.fetch('/api/hello')
expect(updated.headers.get('x-middleware-version')).toBe(
`${initialVersion}-updated`
)
// The unmodified dependency should NOT have been re-evaluated
expect(
updated.headers.get('x-middleware-dep-evaluated-at')
).toBe(initialDepEvaluatedAt)
})
}
)
})
describe('instrumentation hmr', () => {
itTurbopackDev(
'reflects instrumentation module changes via server HMR',
async () => {
const initial = await next
.fetch('/api/instrumentation-state')
.then((res) => res.json())
expect(initial.version).toBe('v0')
await next.patchFile('instrumentation.ts', (content) =>
content.replace("'v0'", "'v1'")
)
await retry(async () => {
const updated = await next
.fetch('/api/instrumentation-state')
.then((res) => res.json())
expect(updated.version).toBe('v1')
})
}
)
itTurbopackDev(
'does not re-evaluate unmodified dependency when instrumentation changes',
async () => {
// Read current state (version may have changed in prior test)
const initial = await next
.fetch('/api/instrumentation-state')
.then((res) => res.json())
const initialDepEvaluatedAt = initial.depEvaluatedAt
expect(String(initialDepEvaluatedAt)).toMatch(/^\d+$/)
// Change only instrumentation, not the dependency
await next.patchFile('instrumentation.ts', (content) =>
content.replace(`'${initial.version}'`, `'${initial.version}-updated'`)
)
await retry(async () => {
const updated = await next
.fetch('/api/instrumentation-state')
.then((res) => res.json())
expect(updated.version).toBe(`${initial.version}-updated`)
// The unmodified dependency should NOT have been re-evaluated
expect(String(updated.depEvaluatedAt)).toBe(
String(initialDepEvaluatedAt)
)
})
}
)
})
describe('route handler hmr', () => {
function getText(res: Response) {
return res.ok
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment