Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Hannes Bornö committed Sep 7, 2022
1 parent 896640d commit a79a3b5
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 151 deletions.
13 changes: 2 additions & 11 deletions packages/next/build/webpack-config.ts
Expand Up @@ -28,7 +28,6 @@ import {
MODERN_BROWSERSLIST_TARGET,
COMPILER_NAMES,
CompilerNameValues,
EDGE_RUNTIME_WEBPACK,
} from '../shared/lib/constants'
import { execOnce } from '../shared/lib/utils'
import { NextConfigComplete } from '../server/config-shared'
Expand Down Expand Up @@ -1408,16 +1407,8 @@ export default async function getBaseWebpackConfig(
// On the server we don't use hashes
filename:
isNodeServer || isEdgeServer
? isEdgeServer
? '[name].js'
: dev
? ({ runtime, chunk }) =>
chunk?.name?.startsWith('pages/')
? runtime === EDGE_RUNTIME_WEBPACK
? // Append the runtime to the filename in dev to prevent both compilers outputting to the same file when switching between runtimes
'[name]-e.js'
: '[name]-s.js'
: '[name].js'
? dev || isEdgeServer
? `[name].js`
: `../[name].js`
: `static/chunks/${isDevFallback ? 'fallback/' : ''}[name]${
dev ? '' : appDir ? '-[chunkhash]' : '-[contenthash]'
Expand Down
140 changes: 0 additions & 140 deletions test/development/edge-api-routes/index.test.ts

This file was deleted.

184 changes: 184 additions & 0 deletions test/e2e/switchable-runtime/index.test.ts
Expand Up @@ -202,6 +202,190 @@ describe('Switchable runtime', () => {
})
}
})

it('should be possible to switch between runtimes in API routes', async () => {
await check(
() => renderViaHTTP(next.url, '/api/switch-in-dev'),
'server response'
)

// Edge
await next.patchFile(
'pages/api/switch-in-dev.js',
`
export const config = {
runtime: 'experimental-edge',
}
export default () => new Response('edge response')
`
)
await check(
() => renderViaHTTP(next.url, '/api/switch-in-dev'),
'edge response'
)

// Server
await next.patchFile(
'pages/api/switch-in-dev.js',
`
export default function (req, res) {
res.send('server response again')
}
`
)
await check(
() => renderViaHTTP(next.url, '/api/switch-in-dev'),
'server response again'
)

// Edge
await next.patchFile(
'pages/api/switch-in-dev.js',
`
export const config = {
runtime: 'experimental-edge',
}
export default () => new Response('edge response again')
`
)
await check(
() => renderViaHTTP(next.url, '/api/switch-in-dev'),
'edge response again'
)
})

it('should be possible to switch between runtimes in pages', async () => {
await check(
() => renderViaHTTP(next.url, '/switch-in-dev'),
/Hello from edge page/
)

// Server
await next.patchFile(
'pages/switch-in-dev.js',
`
export default function Page() {
return <p>Hello from server page</p>
}
`
)
await check(
() => renderViaHTTP(next.url, '/switch-in-dev'),
/Hello from server page/
)

// Edge
await next.patchFile(
'pages/switch-in-dev.js',
`
export default function Page() {
return <p>Hello from edge page again</p>
}
export const config = {
runtime: 'experimental-edge',
}
`
)
await check(
() => renderViaHTTP(next.url, '/switch-in-dev'),
/Hello from edge page again/
)

// Server
await next.patchFile(
'pages/switch-in-dev.js',
`
export default function Page() {
return <p>Hello from server page again</p>
}
`
)
await check(
() => renderViaHTTP(next.url, '/switch-in-dev'),
/Hello from server page again/
)
})

// Doesn't work, see https://github.com/vercel/next.js/pull/39327
it.skip('should be possible to switch between runtimes with same content', async () => {
const fileContent = await next.readFile(
'pages/api/switch-in-dev-same-content.js'
)
console.log({ fileContent })
await check(
() => renderViaHTTP(next.url, '/api/switch-in-dev-same-content'),
'server response'
)

// Edge
await next.patchFile(
'pages/api/switch-in-dev-same-content.js',
`
export const config = {
runtime: 'experimental-edge',
}
export default () => new Response('edge response')
`
)
await check(
() => renderViaHTTP(next.url, '/api/switch-in-dev-same-content'),
'edge response'
)

// Server - same content as first compilation of the server runtime version
await next.patchFile(
'pages/api/switch-in-dev-same-content.js',
fileContent
)
await check(
() => renderViaHTTP(next.url, '/api/switch-in-dev-same-content'),
'server response'
)
})

it('should recover from syntax error when using edge runtime', async () => {
await check(
() => renderViaHTTP(next.url, '/api/syntax-error-in-dev'),
'edge response'
)

// Syntax error
await next.patchFile(
'pages/api/syntax-error-in-dev.js',
`
export const config = {
runtime: 'experimental-edge',
}
export default => new Response('edge response')
`
)
await check(
() => renderViaHTTP(next.url, '/api/syntax-error-in-dev'),
/Unexpected token/
)

// Fix syntax error
await next.patchFile(
'pages/api/syntax-error-in-dev.js',
`
export default () => new Response('edge response again')
export const config = {
runtime: 'experimental-edge',
}
`
)
await check(
() => renderViaHTTP(next.url, '/api/syntax-error-in-dev'),
'edge response again'
)
})
})
} else {
describe('Switchable runtime (prod)', () => {
Expand Down
@@ -0,0 +1,3 @@
export default (req, res) => {
res.send('server response')
}
3 changes: 3 additions & 0 deletions test/e2e/switchable-runtime/pages/api/switch-in-dev.js
@@ -0,0 +1,3 @@
export default (req, res) => {
res.send('server response')
}
5 changes: 5 additions & 0 deletions test/e2e/switchable-runtime/pages/api/syntax-error-in-dev.js
@@ -0,0 +1,5 @@
export default () => new Response('edge response')

export const config = {
runtime: `experimental-edge`,
}
7 changes: 7 additions & 0 deletions test/e2e/switchable-runtime/pages/switch-in-dev.js
@@ -0,0 +1,7 @@
export default function Page() {
return <p>Hello from edge page</p>
}

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

0 comments on commit a79a3b5

Please sign in to comment.