Skip to content

Commit d70423d

Browse files
committed
fix(devserver): fix dev server static data with trailing slash
1 parent 3c16737 commit d70423d

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

src/dev-server/dev-server-utils.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,23 @@ export function isSsrStaticDataPath(pathname: string) {
8888
return fileName === 'page.state.json';
8989
}
9090

91-
export function getSsrStaticDataPath(pathname: string) {
92-
const parts = pathname.split('/');
91+
export function getSsrStaticDataPath(req: d.HttpRequest) {
92+
const parts = req.url.href.split('/');
9393
let fileName = parts[parts.length - 1];
9494
const fileNameParts = fileName.split('?');
9595

9696
parts.pop();
97+
98+
let ssrPath = new URL(parts.join('/')).href;
99+
if (!ssrPath.endsWith('/') && req.headers) {
100+
const h = new Headers(req.headers);
101+
if (h.get('referer').endsWith('/')) {
102+
ssrPath += '/';
103+
}
104+
}
105+
97106
return {
98-
ssrPath: parts.join('/'),
107+
ssrPath,
99108
fileName: fileNameParts[0],
100109
hasQueryString: typeof fileNameParts[1] === 'string' && fileNameParts[1].length > 0,
101110
};

src/dev-server/ssr-request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export async function ssrStaticDataRequest(
7070

7171
if (!diagnostics.some(diagnostic => diagnostic.level === 'error')) {
7272
try {
73-
const { ssrPath, hasQueryString } = getSsrStaticDataPath(req.url.href);
73+
const { ssrPath, hasQueryString } = getSsrStaticDataPath(req);
7474
const url = new URL(ssrPath, req.url);
7575

7676
const opts = getSsrHydrateOptions(devServerConfig, serverCtx, url);

src/dev-server/test/util.spec.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,48 @@ describe('getDevServerClientUrl', () => {
164164
expect(isSsrStaticDataPath('http://stenciljs.com/page.state.json')).toBe(true);
165165
});
166166

167-
it('getSsrStaticDataPath', () => {
168-
let r = getSsrStaticDataPath('http://stenciljs.com/page.static.json');
167+
it('getSsrStaticDataPath, root', () => {
168+
const req: d.HttpRequest = {
169+
url: new URL('http://stenciljs.com/page.static.json'),
170+
method: 'GET',
171+
acceptHeader: '',
172+
searchParams: null,
173+
};
174+
let r = getSsrStaticDataPath(req);
169175
expect(r.fileName).toBe('page.static.json');
170176
expect(r.hasQueryString).toBe(false);
171-
expect(r.ssrPath).toBe('http://stenciljs.com');
177+
expect(r.ssrPath).toBe('http://stenciljs.com/');
178+
});
172179

173-
r = getSsrStaticDataPath('http://stenciljs.com/blog/page.static.json?1234');
180+
it('getSsrStaticDataPath, no trailing slash refer', () => {
181+
const req: d.HttpRequest = {
182+
url: new URL('http://stenciljs.com/blog/page.static.json?v=1234'),
183+
method: 'GET',
184+
acceptHeader: '',
185+
searchParams: null,
186+
headers: {
187+
Referer: 'http://stenciljs.com/page',
188+
},
189+
};
190+
const r = getSsrStaticDataPath(req);
174191
expect(r.fileName).toBe('page.static.json');
175192
expect(r.hasQueryString).toBe(true);
176193
expect(r.ssrPath).toBe('http://stenciljs.com/blog');
177194
});
195+
196+
it('getSsrStaticDataPath, with trailing slash refer', () => {
197+
const req: d.HttpRequest = {
198+
url: new URL('http://stenciljs.com/blog/page.static.json?v=1234'),
199+
method: 'GET',
200+
acceptHeader: '',
201+
searchParams: null,
202+
headers: {
203+
Referer: 'http://stenciljs.com/page/',
204+
},
205+
};
206+
const r = getSsrStaticDataPath(req);
207+
expect(r.fileName).toBe('page.static.json');
208+
expect(r.hasQueryString).toBe(true);
209+
expect(r.ssrPath).toBe('http://stenciljs.com/blog/');
210+
});
178211
});

0 commit comments

Comments
 (0)