Skip to content

Commit 5e28abc

Browse files
committed
always serve absolute cdn url
1 parent 9b9044a commit 5e28abc

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

src/runtime/server/api/app-loader.js.get.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineEventHandler, setResponseHeader } from 'h3'
1+
import { defineEventHandler, setResponseHeader, getRequestURL } from 'h3'
22
import { useRuntimeConfig } from '#imports'
33
// @ts-expect-error - Virtual import
44
import entryPath from '#nuxt-entry-path'
@@ -11,8 +11,17 @@ export default defineEventHandler((event) => {
1111
const buildId = config.app.buildId
1212
const baseURL = config.app.baseURL || '/'
1313
const buildAssetsDir = config.app.buildAssetsDir || '/_nuxt/'
14-
const cdnURL = config.app.cdnURL || ''
15-
const entryPathValue = entryPath
14+
15+
// Set cdnURL - use config if set, otherwise use the current request's origin.
16+
let cdnURL = config.app.cdnURL
17+
if (!cdnURL) {
18+
const requestURL = getRequestURL(event)
19+
cdnURL = requestURL.port
20+
? `${requestURL.protocol}//${requestURL.hostname}:${requestURL.port}`
21+
: `${requestURL.protocol}//${requestURL.host}`
22+
}
23+
24+
const entryPathValue = cdnURL + entryPath
1625

1726
// Serialize public config (with componentPreview enabled)
1827
// Only include what's needed for the client
@@ -39,7 +48,7 @@ export default defineEventHandler((event) => {
3948
teleports.id = 'teleports';
4049
document.body.insertBefore(teleports, nuxt.nextSibling);
4150
42-
// Add Nuxt data script
51+
// Add Nuxt data script - minimal client-side initialization
4352
const nuxtData = document.createElement('script');
4453
nuxtData.type = 'application/json';
4554
nuxtData.setAttribute('data-nuxt-data', 'nuxt-app');

test/basic.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,35 @@ describe('nuxt-component-preview module', async () => {
2929
expect(script).toContain('function initNuxt()')
3030
expect(script).toContain('componentPreview')
3131
})
32+
33+
it('app-loader.js always sets cdnURL', async () => {
34+
const script = await $fetch('/nuxt-component-preview/app-loader.js', {
35+
responseType: 'text',
36+
})
37+
38+
// Check that cdnURL is used in window.__NUXT__.config
39+
expect(script).toContain('window.__NUXT__.config')
40+
expect(script).toContain('cdnURL:')
41+
42+
// Verify the cdnURL is properly set in the generated config
43+
// The script should include cdnURL in the app config section
44+
const configMatch = script.match(/cdnURL:\s*"([^"]*)"/)
45+
expect(configMatch).toBeTruthy()
46+
47+
// The cdnURL should be set to a valid URL (from request origin)
48+
if (configMatch && configMatch[1]) {
49+
const cdnURL = configMatch[1]
50+
expect(cdnURL).toBeTruthy()
51+
// Should be a valid URL with protocol and host
52+
expect(cdnURL).toMatch(/^https?:\/\/[^\/]+/)
53+
}
54+
55+
// Check that entry module src uses the same URL
56+
const entryMatch = script.match(/entry\.src\s*=\s*'([^']+)'/)
57+
expect(entryMatch).toBeTruthy()
58+
if (entryMatch && configMatch) {
59+
// Entry src should start with cdnURL
60+
expect(entryMatch[1]).toContain(configMatch[1])
61+
}
62+
})
3263
})

test/preview-dev.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ describe('preview E2E (dev mode)', async () => {
7979
expect(componentContent.hasCardStructure).toBe(true)
8080
await page.close()
8181
})
82+
8283
})
8384

8485
describe('without app-loader.js (manual setup)', () => {

0 commit comments

Comments
 (0)