Unverified Commit eaa7606d authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix instrumentation with only pages (#62622)

This ensures we properly transpile `instrumentation.ts` when only
`pages` is being used as previously we were relying on the `app`
specific loaders which aren't configured when an `app` directory isn't
present. We regressed on this in
https://github.com/vercel/next.js/pull/60984 as it was working as
expected prior to this commit

x-ref: [slack
thread](https://vercel.slack.com/archives/C011GK1JUBA/p1709075846401649?thread_ts=1706643408.233909&cid=C011GK1JUBA)

Closes NEXT-2632
parent 9408f35b
...@@ -500,6 +500,15 @@ export default async function getBaseWebpackConfig( ...@@ -500,6 +500,15 @@ export default async function getBaseWebpackConfig(
].filter(Boolean) ].filter(Boolean)
: [] : []
const instrumentLayerLoaders = [
// When using Babel, we will have to add the SWC loader
// as an additional pass to handle RSC correctly.
// This will cause some performance overhead but
// acceptable as Babel will not be recommended.
swcServerLayerLoader,
babelLoader,
].filter(Boolean)
const middlewareLayerLoaders = [ const middlewareLayerLoaders = [
// When using Babel, we will have to use SWC to do the optimization // When using Babel, we will have to use SWC to do the optimization
// for middleware to tree shake the unused default optimized imports like "next/server". // for middleware to tree shake the unused default optimized imports like "next/server".
...@@ -1478,7 +1487,7 @@ export default async function getBaseWebpackConfig( ...@@ -1478,7 +1487,7 @@ export default async function getBaseWebpackConfig(
{ {
test: codeCondition.test, test: codeCondition.test,
issuerLayer: WEBPACK_LAYERS.instrument, issuerLayer: WEBPACK_LAYERS.instrument,
use: appServerLayerLoaders, use: instrumentLayerLoaders,
}, },
...(hasAppDir ...(hasAppDir
? [ ? [
......
...@@ -2,11 +2,6 @@ ...@@ -2,11 +2,6 @@
"name": "with-zones", "name": "with-zones",
"version": "1.0.0", "version": "1.0.0",
"private": true, "private": true,
"scripts": {
"dev": "node server.js",
"build": "yarn next build apps/host && yarn next build apps/guest",
"start": "NODE_ENV=production node server.js"
},
"dependencies": { "dependencies": {
"@types/react": "18.0.28", "@types/react": "18.0.28",
"next": "canary", "next": "canary",
......
...@@ -11,9 +11,13 @@ createNextDescribe( ...@@ -11,9 +11,13 @@ createNextDescribe(
startCommand: (global as any).isNextDev ? 'pnpm dev' : 'pnpm start', startCommand: (global as any).isNextDev ? 'pnpm dev' : 'pnpm start',
packageJson: { packageJson: {
scripts: { scripts: {
dev: 'node server.js',
build: 'yarn next build apps/host && yarn next build apps/guest',
start: 'NODE_ENV=production node server.js',
'post-build': 'echo done', 'post-build': 'echo done',
}, },
}, },
dependencies: require('./app/package.json').dependencies,
}, },
({ next, isNextDev }) => { ({ next, isNextDev }) => {
it.each([ it.each([
......
...@@ -8,6 +8,6 @@ export async function generateMetadata() { ...@@ -8,6 +8,6 @@ export async function generateMetadata() {
} }
export default async function Page() { export default async function Page() {
const data = await fetch('https://vercel.com') const data = await fetch('https://example.vercel.sh')
return <pre>RESONSE: {data.status}</pre> return <pre>RESONSE: {data.status}</pre>
} }
...@@ -6,6 +6,11 @@ export async function generateMetadata() { ...@@ -6,6 +6,11 @@ export async function generateMetadata() {
} }
export default async function Page() { export default async function Page() {
const data = await fetch('https://vercel.com') const data = await fetch('https://example.vercel.sh')
return <pre>{await data.text()}</pre> return (
<>
<p>Page</p>
<pre>{await data.text()}</pre>
</>
)
} }
export async function register() {
console.log('instrumentation log')
}
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
for (const { app, src, pathname, text } of [
{
pages: true,
src: false,
pathname: '/pages/param/getServerSideProps',
text: 'Page',
},
{
pages: true,
src: true,
pathname: '/pages/param/getServerSideProps',
text: 'Page',
},
{
app: true,
src: false,
pathname: '/app/param/rsc-fetch',
text: 'Page',
},
{
app: true,
src: true,
pathname: '/app/param/rsc-fetch',
text: 'Page',
},
]) {
describe(`instrumentation ${app ? 'app' : 'pages'}${
src ? ' src/' : ''
}`, () => {
const curDir = app ? 'app' : 'pages'
const oppositeDir = app ? 'pages' : 'app'
const { next } = nextTestSetup({
files: __dirname,
env: {
NEXT_PUBLIC_SIMPLE_INSTRUMENT: '1',
},
skipDeployment: true,
packageJson: {
scripts: {
'setup-dir': `mv instrumentation-minimal.ts instrumentation.ts; rm -rf ${oppositeDir}${
src
? `; mkdir src; mv ${curDir} src/; mv instrumentation.ts src/`
: ''
}`,
dev: 'pnpm setup-dir && next dev',
build: 'pnpm setup-dir && next build',
start: 'next start',
},
},
startCommand: `pnpm ${(global as any).isNextDev ? 'dev' : 'start'}`,
buildCommand: `pnpm build`,
dependencies: require('./package.json').dependencies,
})
it('should start and serve correctly', async () => {
const html = await next.render(pathname)
expect(html).toContain(text)
retry(() => {
expect(next.cliOutput).toContain('instrumentation log')
})
})
})
}
import { register as registerForTest } from './instrumentation-test'
export async function register() { export async function register() {
const { register: registerForTest } = await import('./instrumentation-test')
if (process.env.__NEXT_TEST_MODE) { if (process.env.__NEXT_TEST_MODE) {
registerForTest() registerForTest()
} else if (process.env.NEXT_RUNTIME === 'nodejs') { } else if (process.env.NEXT_RUNTIME === 'nodejs') {
......
...@@ -129,12 +129,13 @@ createNextDescribe( ...@@ -129,12 +129,13 @@ createNextDescribe(
], ],
}, },
{ {
name: 'fetch GET https://vercel.com/', name: 'fetch GET https://example.vercel.sh/',
attributes: { attributes: {
'http.method': 'GET', 'http.method': 'GET',
'http.url': 'https://vercel.com/', 'http.url': 'https://example.vercel.sh/',
'net.peer.name': 'vercel.com', 'net.peer.name': 'example.vercel.sh',
'next.span_name': 'fetch GET https://vercel.com/', 'next.span_name':
'fetch GET https://example.vercel.sh/',
'next.span_type': 'AppRender.fetch', 'next.span_type': 'AppRender.fetch',
}, },
kind: 2, kind: 2,
...@@ -265,14 +266,15 @@ createNextDescribe( ...@@ -265,14 +266,15 @@ createNextDescribe(
], ],
}, },
{ {
name: 'fetch GET https://vercel.com/', name: 'fetch GET https://example.vercel.sh/',
kind: 2, kind: 2,
attributes: { attributes: {
'next.span_name': 'fetch GET https://vercel.com/', 'next.span_name':
'fetch GET https://example.vercel.sh/',
'next.span_type': 'AppRender.fetch', 'next.span_type': 'AppRender.fetch',
'http.url': 'https://vercel.com/', 'http.url': 'https://example.vercel.sh/',
'http.method': 'GET', 'http.method': 'GET',
'net.peer.name': 'vercel.com', 'net.peer.name': 'example.vercel.sh',
}, },
status: { code: 0 }, status: { code: 0 },
}, },
......
...@@ -73,6 +73,8 @@ export class NextInstance { ...@@ -73,6 +73,8 @@ export class NextInstance {
this.env = {} this.env = {}
Object.assign(this, opts) Object.assign(this, opts)
require('console').log('packageJson??', this.packageJson)
if (!(global as any).isNextDeploy) { if (!(global as any).isNextDeploy) {
this.env = { this.env = {
...this.env, ...this.env,
...@@ -102,7 +104,17 @@ export class NextInstance { ...@@ -102,7 +104,17 @@ export class NextInstance {
) )
} }
await fs.cp(files.fsPath, this.testDir, { recursive: true }) await fs.cp(files.fsPath, this.testDir, {
recursive: true,
filter(source) {
// we don't copy a package.json as it's manually written
// via the createNextInstall process
if (path.relative(files.fsPath, source) === 'package.json') {
return false
}
return true
},
})
} else { } else {
for (const filename of Object.keys(files)) { for (const filename of Object.keys(files)) {
const item = files[filename] const item = files[filename]
......
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