Skip to content

Commit 4d004aa

Browse files
committed
[scramjet/core] fix same regex mistake in unrewriteLinkHeader, add more tests to linkheader.ts
1 parent b371467 commit 4d004aa

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

packages/scramjet/packages/core/src/client/shared/requests/xmlhttprequest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,6 @@ export default function (client: ScramjetClient, self: Self) {
158158
export function unrewriteLinkHeader(header: string, context: ScramjetContext) {
159159
return header.replace(
160160
/<([^>]+)>/gi,
161-
(match) => `${unrewriteUrl(match, context)}`
161+
(_match, p1) => `<${unrewriteUrl(p1, context)}>`
162162
);
163163
}

packages/scramjet/packages/runway/src/tests/linkheader.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,71 @@ export default [
2323
});
2424
},
2525
}),
26+
serverTest({
27+
name: "linkheader-multiple-preloads",
28+
start: async (server, port, { pass, fail }) => {
29+
const loaded = new Set<string>();
30+
server.on("request", (req, res) => {
31+
if (req.url === "/") {
32+
res.writeHead(200, {
33+
"Content-Type": "text/html",
34+
Link: `<http://localhost:${port}/script1.js>; rel="preload"; as="script", <http://localhost:${port}/script2.js>; rel="preload"; as="script"`,
35+
});
36+
res.end();
37+
} else if (req.url === "/script1.js") {
38+
loaded.add("script1");
39+
res.writeHead(200, { "Content-Type": "application/javascript" });
40+
res.end("console.log('script1');");
41+
if (loaded.size === 2) {
42+
pass("both scripts preloaded");
43+
}
44+
} else if (req.url === "/script2.js") {
45+
loaded.add("script2");
46+
res.writeHead(200, { "Content-Type": "application/javascript" });
47+
res.end("console.log('script2');");
48+
if (loaded.size === 2) {
49+
pass("both scripts preloaded");
50+
}
51+
} else {
52+
res.writeHead(404);
53+
res.end("Not found");
54+
fail(`unexpected url request: ${req.url}`);
55+
}
56+
});
57+
},
58+
}),
59+
serverTest({
60+
name: "linkheader-next-prev",
61+
start: async (server, port) => {
62+
const link = `<http://localhost:${port}/page2>; rel="next", <http://localhost:${port}/page0>; rel="prev"`;
63+
server.on("request", (req, res) => {
64+
if (req.url === "/") {
65+
res.writeHead(200, {
66+
"Content-Type": "text/html",
67+
});
68+
res.end(`
69+
<script>
70+
fetch("/page1").then(res => {
71+
let next = res.headers.get("Link");
72+
const expected = '${link}';
73+
if (next === expected) {
74+
__testPass("next link header is correct");
75+
} else {
76+
__testFail("next link header is incorrect", { actual: next, expected: expected });
77+
}
78+
});
79+
</script>
80+
`);
81+
} else if (req.url === "/page1") {
82+
res.writeHead(200, {
83+
Link: link,
84+
});
85+
res.end("Page 1");
86+
} else {
87+
res.writeHead(404);
88+
res.end("Not found");
89+
}
90+
});
91+
},
92+
}),
2693
];

0 commit comments

Comments
 (0)