Skip to content

Commit ebe92ea

Browse files
authored
fix(ui): Fix dynamic publicPath generation (#7496)
1 parent 8aae4fd commit ebe92ea

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

.changeset/slimy-guests-leave.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
'@clerk/ui': patch
3+
---

packages/ui/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
},
1010
"license": "MIT",
1111
"author": "Clerk",
12-
"sideEffects": false,
12+
"sideEffects": [
13+
"./src/utils/setWebpackChunkPublicPath.ts"
14+
],
1315
"type": "module",
1416
"exports": {
1517
".": {

packages/ui/src/utils/setWebpackChunkPublicPath.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,37 @@
22
* A combination of settings a fixed __PKG_VERSION__ during build time
33
* and the runtime __webpack_public_path__ config option (https://webpack.js.org/guides/public-path/).
44
* We explicitly set the public path in order to account for all chunk caching scenarios.
5-
* A specific version of clerk.browser.js file should always try to load its corresponding chunk versions,
5+
* A specific version of ui.browser.js file should always try to load its corresponding chunk versions,
66
* otherwise we risk hitting caching issues.
77
*
88
* Scenario:
9-
* 1. We release clerk-js@1.0.0 containing: clerk.browser.js, chunkA-1.0.0.js, chunkB-1.0.0.js
9+
* 1. We release @clerk/ui@1.0.0 containing: ui.browser.js, chunkA-1.0.0.js, chunkB-1.0.0.js
1010
* 2. A user opens an app using Clerk
11-
* 3. The browser downloads and caches `/npm/@clerk/clerk-js@latest/dist/clerk.browser.js`
12-
* 4. chunkA is needed so the browser downloads and caches `/npm/@clerk/clerk-js@latest/dist/chunkA-1.0.0.js`
13-
* 5. Meanwhile, we release clerk-js@1.2.0 containing: clerk.browser.js, chunkA-1.2.0.js, chunkB-1.2.0.js
14-
* On our CDN, @clerk/clerk-js@latest now points to the new version
11+
* 3. The browser downloads and caches `/npm/@clerk/ui@latest/dist/ui.browser.js`
12+
* 4. chunkA is needed so the browser downloads and caches `/npm/@clerk/ui@latest/dist/chunkA-1.0.0.js`
13+
* 5. Meanwhile, we release ui@1.2.0 containing: ui.browser.js, chunkA-1.2.0.js, chunkB-1.2.0.js
14+
* On our CDN, @clerk/ui@latest now points to the new version
1515
* 6. A user opens the app again
16-
* 7. The browser loads `/npm/@clerk/clerk-js@latest/dist/clerk.browser.js` FROM CACHE (v1.0.0 file)
17-
* 8. chunkA is needed so the browser loads `/npm/@clerk/clerk-js@latest/dist/chunkA-1.0.0.js` FROM CACHE (v1.0.0 file)
18-
* 9. chunkB is needed for the first time. The cached v1.0.0 clerk.browser.js will try to load (request)
19-
* `/npm/@clerk/clerk-js@latest/dist/chunkA-1.0.0.js` but because clerk-js@latest now resolves to v1.2.0,
16+
* 7. The browser loads `/npm/@clerk/ui@latest/dist/ui.browser.js` FROM CACHE (v1.0.0 file)
17+
* 8. chunkA is needed so the browser loads `/npm/@clerk/ui@latest/dist/chunkA-1.0.0.js` FROM CACHE (v1.0.0 file)
18+
* 9. chunkB is needed for the first time. The cached v1.0.0 ui.browser.js will try to load (request)
19+
* `/npm/@clerk/ui@latest/dist/chunkA-1.0.0.js` but because ui@latest now resolves to v1.2.0,
2020
* the v1.0.0 file will not be found and the app will crash
2121
*
2222
* Solution:
23-
* A given clerk.browser.js file will only load its corresponding chunks using a fixed version. Example:
24-
* - clerk.browser.js loads from https://pk.accounts.dev/npm/@clerk/clerk-js@canary/dist/clerk.browser.js
25-
* - all other chunks need to be loaded from https://pk.accounts.dev/npm/@clerk/clerk-js@__PKG_VERSION__/dist/
23+
* A given ui.browser.js file will only load its corresponding chunks using a fixed version. Example:
24+
* - ui.browser.js loads from https://pk.accounts.dev/npm/@clerk/ui@canary/dist/ui.browser.js
25+
* - all other chunks need to be loaded from https://pk.accounts.dev/npm/@clerk/ui@__PKG_VERSION__/dist/
2626
*/
2727
if (!__DEV__) {
28-
const CLERKJS_NPM_PATH_REGEX = /(^.*\/@clerk\/clerk-js@)(.+?)(\/dist.*)/;
28+
const CLERK_UI_NPM_PATH_REGEX = /(^.*\/@clerk\/ui@)(.+?)(\/dist.*)/;
2929
const setWebpackChunkPublicPath = () => {
3030
try {
3131
// @ts-expect-error
3232
const scriptUrl = new URL(document.currentScript.src);
3333
let hrefWithoutFilename = new URL(scriptUrl.href.split('/').slice(0, -1).join('/')).href;
3434
hrefWithoutFilename += hrefWithoutFilename.endsWith('/') ? '' : '/';
35-
__webpack_public_path__ = hrefWithoutFilename.replace(CLERKJS_NPM_PATH_REGEX, `$1${__PKG_VERSION__}$3`);
35+
__webpack_public_path__ = hrefWithoutFilename.replace(CLERK_UI_NPM_PATH_REGEX, `$1${__PKG_VERSION__}$3`);
3636
} catch {
3737
//
3838
}

0 commit comments

Comments
 (0)