Skip to content
Merged
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
8 changes: 6 additions & 2 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,8 @@ export function publicFileToBuiltUrl(
): string {
if (config.command !== 'build') {
// We don't need relative base or renderBuiltUrl support during dev
return joinUrlSegments(config.decodedBase, url)
const base = joinUrlSegments(config.server.origin ?? '', config.decodedBase)
return joinUrlSegments(base, url)
}
const hash = getHash(url)
let cache = publicAssetUrlCache.get(config)
Expand Down Expand Up @@ -729,7 +730,10 @@ export function toOutputFilePathInJSForBundledDev(
)
// renderBuiltUrl is not supported in bundled dev
if (typeof outputUrl === 'object') throw new Error('unreachable')
return outputUrl
return joinUrlSegments(
environment.getTopLevelConfig().server.origin ?? '',
outputUrl,
)
}

export async function urlToBuiltUrl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,33 @@ test('should have no 404s', windowsTodo, () => {
})

describe('asset imports from js', () => {
test(
'file outside root',
bundledDevTodo(
'bundled dev does not apply `server.origin` to emitted asset URLs',
),
async () => {
// assert valid image src https://github.com/microsoft/playwright/issues/6046#issuecomment-1799585719
await vi.waitUntil(() =>
page
.locator('.asset-reference.outside-root .asset-preview')
.evaluate((el: HTMLImageElement) => el.naturalWidth > 0),
)
test('file outside root', windowsTodo, async () => {
// assert valid image src https://github.com/microsoft/playwright/issues/6046#issuecomment-1799585719
await vi.waitUntil(() =>
page
.locator('.asset-reference.outside-root .asset-preview')
.evaluate((el: HTMLImageElement) => el.naturalWidth > 0),
)

const text = await page.textContent(
'.asset-reference.outside-root .asset-url',
const text = await page.textContent(
'.asset-reference.outside-root .asset-url',
)
if (isBuild) {
expect(text).toMatch(/\/dev\/assets\/logo-[-\w]{8}\.png/)
} else if (isBundledDev) {
// asset url is prefixed with server.origin
expect(text).toMatch(
`http://localhost:${ports['backend-integration']}/dev/assets/`,
)
if (isBuild) {
expect(text).toMatch(/\/dev\/assets\/logo-[-\w]{8}\.png/)
} else if (isBundledDev) {
// asset url is prefixed with server.origin
expect(text).toMatch(
`http://localhost:${ports['backend-integration']}/dev/assets/`,
)
expect(text).toMatch(/\/dev\/assets\/logo-[-\w]{8}\.png/)
} else {
// asset url is prefixed with server.origin
expect(text).toMatch(
`http://localhost:${ports['backend-integration']}/dev/@fs/`,
)
expect(text).toMatch(/\/dev\/@fs\/.+?\/images\/logo\.png/)
}
},
)
expect(text).toMatch(/\/dev\/assets\/logo-[-\w]{8}\.png/)
} else {
// asset url is prefixed with server.origin
expect(text).toMatch(
`http://localhost:${ports['backend-integration']}/dev/@fs/`,
)
expect(text).toMatch(/\/dev\/@fs\/.+?\/images\/logo\.png/)
}
})
})

describe.runIf(isBuild)('build', () => {
Expand Down Expand Up @@ -146,9 +140,7 @@ describe.runIf(isServe)('serve', () => {

test(
'server.origin is applied to non-public CSS url()',
bundledDevTodo(
'bundled dev does not apply `server.origin` to emitted asset URLs',
),
windowsTodo,
async () => {
const bg = await getCssRuleBg('.outside-root--aliased')
expect(bg).toContain(
Expand Down
Loading