Skip to content

Commit eec59e9

Browse files
authored
Merge branch 'master' into svg-links
2 parents f2e51e7 + ebf9010 commit eec59e9

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ declare module 'swup' {
2626
}
2727
export interface HookDefinitions {
2828
'link:hover': { el: HTMLAnchorElement | SVGAElement; event: DelegateEvent };
29-
'page:preload': { page?: PageData };
29+
'page:preload': { url: string, page?: PageData };
3030
}
3131
export interface HookReturnValues {
3232
'page:preload': Promise<PageData>;
@@ -400,9 +400,10 @@ export default class SwupPreloadPlugin extends Plugin {
400400
/**
401401
* Perform the actual preload fetch and trigger the preload hook.
402402
*/
403-
protected async performPreload(url: string): Promise<PageData> {
404-
const page = await this.swup.hooks.call('page:preload', undefined, {}, async (visit, args) => {
405-
args.page = await this.swup.fetchPage(url);
403+
protected async performPreload(href: string): Promise<PageData> {
404+
const { url } = Location.fromUrl(href);
405+
const page = await this.swup.hooks.call('page:preload', undefined, { url }, async (visit, args) => {
406+
args.page = await this.swup.fetchPage(href);
406407
return args.page;
407408
});
408409
return page;

tests/fixtures/origins.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1" />
6+
<title>Origins</title>
7+
<link rel="stylesheet" href="/assets/style.css" />
8+
</head>
9+
<body>
10+
<main id="swup" class="transition-main">
11+
<h1>Origins</h1>
12+
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
13+
<ul>
14+
<li><a href="/page-1.html">Local, relative</a></li>
15+
<li><a href="http://localhost:8274/page-2.html">Local, absolute</a></li>
16+
<li><a href="https://example.net/page-3.html">External</a></li>
17+
</ul>
18+
</main>
19+
<script src="/dist/swup.umd.js"></script>
20+
<script src="/dist/index.umd.js"></script>
21+
<script>
22+
window._swup = new Swup({
23+
plugins: [new SwupPreloadPlugin()]
24+
});
25+
</script>
26+
</body>
27+
</html>

tests/functional/preload-plugin.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,28 @@ test.describe('hooks', () => {
207207
await expect(async () => expect(await triggered()).toBe(true)).toPass();
208208
});
209209
});
210+
211+
test.describe('ignores external origins', () => {
212+
test.beforeEach(async ({ page }) => {
213+
await page.goto('/origins.html');
214+
await waitForSwup(page);
215+
await page.evaluate(() => {
216+
window.data = [];
217+
window._swup.hooks.before('page:preload', (visit, { url }) => (window.data.push(url)));
218+
});
219+
});
220+
test('ignores link elements with external origin', async ({ page }) => {
221+
await page.focus('a[href$="/page-1.html"]');
222+
await page.focus('a[href$="/page-2.html"]');
223+
await page.focus('a[href$="/page-3.html"]');
224+
const urls = await page.evaluate(() => window.data);
225+
expect(urls).toEqual(['/page-1.html', '/page-2.html']);
226+
});
227+
test('ignores preload requests with external origin', async ({ page }) => {
228+
await page.evaluate(() => {
229+
window._swup.preload!(['https://example.net/page-3.html', '/page-1.html', 'page-2.html']);
230+
});
231+
const urls = await page.evaluate(() => window.data);
232+
expect(urls).toEqual(['/page-1.html', '/page-2.html']);
233+
});
234+
});

0 commit comments

Comments
 (0)