Skip to content

Commit

Permalink
Merge branch 'master' into svg-links
Browse files Browse the repository at this point in the history
  • Loading branch information
daun authored Jan 27, 2024
2 parents f2e51e7 + ebf9010 commit eec59e9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ declare module 'swup' {
}
export interface HookDefinitions {
'link:hover': { el: HTMLAnchorElement | SVGAElement; event: DelegateEvent };
'page:preload': { page?: PageData };
'page:preload': { url: string, page?: PageData };
}
export interface HookReturnValues {
'page:preload': Promise<PageData>;
Expand Down Expand Up @@ -400,9 +400,10 @@ export default class SwupPreloadPlugin extends Plugin {
/**
* Perform the actual preload fetch and trigger the preload hook.
*/
protected async performPreload(url: string): Promise<PageData> {
const page = await this.swup.hooks.call('page:preload', undefined, {}, async (visit, args) => {
args.page = await this.swup.fetchPage(url);
protected async performPreload(href: string): Promise<PageData> {
const { url } = Location.fromUrl(href);
const page = await this.swup.hooks.call('page:preload', undefined, { url }, async (visit, args) => {
args.page = await this.swup.fetchPage(href);
return args.page;
});
return page;
Expand Down
27 changes: 27 additions & 0 deletions tests/fixtures/origins.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Origins</title>
<link rel="stylesheet" href="/assets/style.css" />
</head>
<body>
<main id="swup" class="transition-main">
<h1>Origins</h1>
<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>
<ul>
<li><a href="/page-1.html">Local, relative</a></li>
<li><a href="http://localhost:8274/page-2.html">Local, absolute</a></li>
<li><a href="https://example.net/page-3.html">External</a></li>
</ul>
</main>
<script src="/dist/swup.umd.js"></script>
<script src="/dist/index.umd.js"></script>
<script>
window._swup = new Swup({
plugins: [new SwupPreloadPlugin()]
});
</script>
</body>
</html>
25 changes: 25 additions & 0 deletions tests/functional/preload-plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,28 @@ test.describe('hooks', () => {
await expect(async () => expect(await triggered()).toBe(true)).toPass();
});
});

test.describe('ignores external origins', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/origins.html');
await waitForSwup(page);
await page.evaluate(() => {
window.data = [];
window._swup.hooks.before('page:preload', (visit, { url }) => (window.data.push(url)));
});
});
test('ignores link elements with external origin', async ({ page }) => {
await page.focus('a[href$="/page-1.html"]');
await page.focus('a[href$="/page-2.html"]');
await page.focus('a[href$="/page-3.html"]');
const urls = await page.evaluate(() => window.data);
expect(urls).toEqual(['/page-1.html', '/page-2.html']);
});
test('ignores preload requests with external origin', async ({ page }) => {
await page.evaluate(() => {
window._swup.preload!(['https://example.net/page-3.html', '/page-1.html', 'page-2.html']);
});
const urls = await page.evaluate(() => window.data);
expect(urls).toEqual(['/page-1.html', '/page-2.html']);
});
});

0 comments on commit eec59e9

Please sign in to comment.