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

Fix serverside asset modules #27413

Closed
wants to merge 8 commits into from
Closed
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
4 changes: 1 addition & 3 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1000,9 +1000,7 @@ export default async function getBaseWebpackConfig(
],
},
output: {
// we must set publicPath to an empty value to override the default of
// auto which doesn't work in IE11
publicPath: `${config.assetPrefix || ''}/_next/`,
publicPath: isServer ? '' : `${config.assetPrefix || ''}/_next/`,
path:
isServer && isWebpack5 && !dev
? path.join(outputPath, 'chunks')
Expand Down
3 changes: 3 additions & 0 deletions test/integration/server-asset-modules/my-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"message": "hello world"
}
1 change: 1 addition & 0 deletions test/integration/server-asset-modules/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
6 changes: 6 additions & 0 deletions test/integration/server-asset-modules/pages/api/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as fs from 'fs/promises'
export default async (req, res) => {
const fileUrl = new URL('../../my-data.json', import.meta.url)
const content = await fs.readFile(fileUrl, { encoding: 'utf-8' })
res.json(JSON.parse(content))
}
80 changes: 80 additions & 0 deletions test/integration/server-asset-modules/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* eslint-env jest */

import fs from 'fs-extra'
import {
fetchViaHTTP,
findPort,
killApp,
launchApp,
nextBuild,
nextStart,
} from 'next-test-utils'
import { join } from 'path'

jest.setTimeout(1000 * 60 * 2)

let app
let appPort
const appDir = join(__dirname, '../')

function runTests() {
it('should enable reading local files in api routes', async () => {
const res = await fetchViaHTTP(appPort, '/api/test', null, {})
expect(res.status).toEqual(200)
const content = await res.json()
expect(content).toHaveProperty('message', 'hello world')
})
}

const nextConfig = join(appDir, 'next.config.js')

describe('serverside asset modules', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))

runTests()
})

describe('production mode', () => {
beforeAll(async () => {
const curConfig = await fs.readFile(nextConfig, 'utf8')

if (curConfig.includes('target')) {
await fs.writeFile(nextConfig, `module.exports = {}`)
}
await nextBuild(appDir)

appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))

runTests()
})

describe('serverless mode', () => {
let origNextConfig

beforeAll(async () => {
origNextConfig = await fs.readFile(nextConfig, 'utf8')
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)

await nextBuild(appDir)

appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await fs.writeFile(nextConfig, origNextConfig)
await killApp(app)
})
runTests()
})
})