Skip to content

Commit 943ece1

Browse files
authored
fix: allow getting URL of JS files in publicDir (#17915)
1 parent 8c661b2 commit 943ece1

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

packages/vite/src/node/server/middlewares/static.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
isSameFileUri,
1717
normalizePath,
1818
removeLeadingSlash,
19+
urlRE,
1920
} from '../../utils'
2021
import {
2122
cleanUrl,
@@ -86,7 +87,9 @@ export function servePublicMiddleware(
8687
if (
8788
(publicFiles && !publicFiles.has(toFilePath(req.url!))) ||
8889
isImportRequest(req.url!) ||
89-
isInternalRequest(req.url!)
90+
isInternalRequest(req.url!) ||
91+
// for `/public-file.js?url` to be transformed
92+
urlRE.test(req.url!)
9093
) {
9194
return next()
9295
}

playground/assets/__tests__/assets.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,18 @@ describe('asset imports from js', () => {
143143
"
144144
`)
145145
})
146+
147+
test('from /public (js)', async () => {
148+
expect(await page.textContent('.public-js-import')).toMatch(
149+
'/foo/bar/raw.js',
150+
)
151+
expect(await page.textContent('.public-js-import-content'))
152+
.toMatchInlineSnapshot(`
153+
"document.querySelector('.raw-js').textContent =
154+
'[success] Raw js from /public loaded'
155+
"
156+
`)
157+
})
146158
})
147159

148160
describe('css url() references', () => {

playground/assets/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ <h2>Asset Imports from JS</h2>
3131
From publicDir (json): <code class="public-json-import"></code> Content:
3232
<code class="public-json-import-content"></code>
3333
</li>
34+
<li>
35+
From publicDir (js): <code class="public-js-import"></code> Content:
36+
<code class="public-js-import-content"></code>
37+
</li>
3438
</ul>
3539

3640
<h2>CSS url references</h2>
@@ -441,6 +445,13 @@ <h3>assets in noscript</h3>
441445
text('.public-json-import-content', await res.text())
442446
})()
443447

448+
import publicJsUrl from '/raw.js?url'
449+
text('.public-js-import', publicJsUrl)
450+
;(async () => {
451+
const res = await fetch(publicJsUrl)
452+
text('.public-js-import-content', await res.text())
453+
})()
454+
444455
import svgFrag from './nested/fragment.svg'
445456
text('.svg-frag-import-path', svgFrag)
446457
document.querySelector('.svg-frag-import').src = svgFrag + '#icon-heart-view'

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy