Skip to content

Commit 20b35b8

Browse files
authored
fix(domain): assetPrefix is prefixed by "//" (#3)
* fix: // * fix: // * chore: update
1 parent d54d4fc commit 20b35b8

File tree

4 files changed

+57
-8
lines changed

4 files changed

+57
-8
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,16 @@ Specifies the retry domain when assets fail to load. In the `domain` array, the
9999
For example:
100100

101101
```js
102-
pluginAssetsRetry({
103-
domain: ["https://cdn1.com", "https://cdn2.com", "https://cdn3.com"],
102+
// rsbuild.config.ts
103+
defineConfig({
104+
plugins: [
105+
pluginAssetsRetry({
106+
domain: ["https://cdn1.com", "https://cdn2.com", "https://cdn3.com"],
107+
})
108+
],
109+
output: {
110+
assetPrefix: "https://cdn1.com",
111+
},
104112
});
105113
```
106114

README.zh-CN.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,16 @@ const defaultOptions = {
9797
比如:
9898

9999
```js
100-
pluginAssetsRetry({
101-
domain: ["https://cdn1.com", "https://cdn2.com", "https://cdn3.com"],
100+
// rsbuild.config.ts
101+
defineConfig({
102+
plugins: [
103+
pluginAssetsRetry({
104+
domain: ["https://cdn1.com", "https://cdn2.com", "https://cdn3.com"],
105+
})
106+
],
107+
output: {
108+
assetPrefix: "https://cdn1.com",
109+
},
102110
});
103111
```
104112

src/runtime/asyncChunkRetry.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,14 @@ function initRetry(chunkId: string, isCssAsyncChunk: boolean): Retry {
140140
}
141141

142142
const originalPublicPath = __RUNTIME_GLOBALS_PUBLIC_PATH__;
143-
const originalSrcUrl = originalPublicPath.startsWith('/')
144-
? window.origin + originalPublicPath + originalScriptFilename
145-
: originalPublicPath + originalScriptFilename;
143+
const originalSrcUrl =
144+
originalPublicPath[0] === '/' && originalPublicPath[1] !== '/'
145+
? window.origin + originalPublicPath + originalScriptFilename
146+
: originalPublicPath + originalScriptFilename;
146147
const originalQuery = getQueryFromUrl(originalSrcUrl);
147148

148149
const existRetryTimes = 0;
149-
const nextDomain = config.domain?.[0] || window.origin;
150+
const nextDomain = findCurrentDomain(originalSrcUrl);
150151

151152
return {
152153
nextDomain,

test/index.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,35 @@ test('should work when the first, second cdn are all failed and the third is suc
112112
await expect(compTestElement).toHaveText('Hello AsyncCompTest');
113113
await expect(compTestElement).toHaveCSS('background-color', 'rgb(0, 0, 139)');
114114
});
115+
116+
test('should work when the first, second cdn are all failed and the third is success, domain is prefixed with //', async ({
117+
page,
118+
}) => {
119+
// this is a real world case for assets-retry
120+
const port = await getRandomPort();
121+
const rsbuild = await createRsbuildWithMiddleware(
122+
[],
123+
{
124+
minify: true,
125+
domain: ['a.com/foo-path', 'b.com', `localhost:${port}`],
126+
addQuery: true,
127+
onRetry(context) {
128+
console.info('onRetry', context);
129+
},
130+
onSuccess(context) {
131+
console.info('onSuccess', context);
132+
},
133+
onFail(context) {
134+
console.info('onFail', context);
135+
},
136+
},
137+
undefined,
138+
port,
139+
'//a.com/foo-path',
140+
);
141+
142+
await gotoPage(page, rsbuild);
143+
const compTestElement = page.locator('#async-comp-test');
144+
await expect(compTestElement).toHaveText('Hello AsyncCompTest');
145+
await expect(compTestElement).toHaveCSS('background-color', 'rgb(0, 0, 139)');
146+
});

0 commit comments

Comments
 (0)