-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sarah Rainsberger <[email protected]>
- Loading branch information
1 parent
23c9a30
commit 38e21d1
Showing
23 changed files
with
688 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Prefetching is now supported in core | ||
|
||
You can enable prefetching for your site with the `prefetch: true` config. It is enabled by default when using [View Transitions](https://docs.astro.build/en/guides/view-transitions/) and can also be used to configure the `prefetch` behaviour used by View Transitions. | ||
|
||
You can enable prefetching by setting `prefetch:true` in your Astro config: | ||
|
||
```js | ||
// astro.config.js | ||
import { defineConfig } from 'astro/config'; | ||
|
||
export default defineConfig({ | ||
prefetch: true | ||
}) | ||
``` | ||
|
||
This replaces the `@astrojs/prefetch` integration, which is now deprecated and will eventually be removed. | ||
Visit the [Prefetch guide](https://docs.astro.build/en/guides/prefetch/) for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { defineConfig } from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
prefetch: true | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@e2e/prefetch", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*" | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/astro/e2e/fixtures/prefetch/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
</head> | ||
<body> | ||
<h1>Prefetch</h1> | ||
<a id="prefetch-default" href="/prefetch-default">default</a> | ||
<br> | ||
<a id="prefetch-false" href="/prefetch-false" data-astro-prefetch="false">false</a> | ||
<br> | ||
<a id="prefetch-tap" href="/prefetch-tap" data-astro-prefetch="tap">tap</a> | ||
<br> | ||
<a id="prefetch-hover" href="/prefetch-hover" data-astro-prefetch="hover">hover</a> | ||
<br> | ||
<button id="prefetch-manual">manual</button> | ||
<br> | ||
<span>Scroll down to trigger viewport prefetch</span> | ||
<!-- Large empty space to test viewport --> | ||
<div style="height: 1000px;"></div> | ||
<a id="prefetch-viewport" href="/prefetch-viewport" data-astro-prefetch="viewport">viewport</a> | ||
<script> | ||
// @ts-nocheck | ||
import { prefetch } from 'astro:prefetch' | ||
document.getElementById('prefetch-manual').addEventListener('click', () => { | ||
prefetch('/prefetch-manual', { with: 'link' }) | ||
}) | ||
</script> | ||
</body> | ||
</html> |
1 change: 1 addition & 0 deletions
1
packages/astro/e2e/fixtures/prefetch/src/pages/prefetch-default.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>Prefetch default</h1> |
1 change: 1 addition & 0 deletions
1
packages/astro/e2e/fixtures/prefetch/src/pages/prefetch-false.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>Prefetch false</h1> |
1 change: 1 addition & 0 deletions
1
packages/astro/e2e/fixtures/prefetch/src/pages/prefetch-hover.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>Prefetch hover</h1> |
1 change: 1 addition & 0 deletions
1
packages/astro/e2e/fixtures/prefetch/src/pages/prefetch-tap.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>Prefetch tap</h1> |
1 change: 1 addition & 0 deletions
1
packages/astro/e2e/fixtures/prefetch/src/pages/prefetch-viewport.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>Prefetch viewport</h1> |
6 changes: 6 additions & 0 deletions
6
packages/astro/e2e/fixtures/view-transitions/src/pages/prefetch.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
import Layout from '../components/Layout.astro'; | ||
--- | ||
<Layout> | ||
<a id="prefetch-one" href="/one">Go to one with prefetch on hover</a> | ||
</Layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import { expect } from '@playwright/test'; | ||
import { testFactory } from './test-utils.js'; | ||
|
||
const test = testFactory({ | ||
root: './fixtures/prefetch/', | ||
}); | ||
|
||
test.describe('Prefetch (default)', () => { | ||
let devServer; | ||
/** @type {string[]} */ | ||
const reqUrls = []; | ||
|
||
test.beforeAll(async ({ astro }) => { | ||
devServer = await astro.startDevServer(); | ||
}); | ||
|
||
test.beforeEach(async ({ page }) => { | ||
page.on('request', (req) => { | ||
reqUrls.push(new URL(req.url()).pathname); | ||
}); | ||
}); | ||
|
||
test.afterEach(() => { | ||
reqUrls.length = 0; | ||
}); | ||
|
||
test.afterAll(async () => { | ||
await devServer.stop(); | ||
}); | ||
|
||
test('Link without data-astro-prefetch should not prefetch', async ({ page, astro }) => { | ||
await page.goto(astro.resolveUrl('/')); | ||
expect(reqUrls).not.toContainEqual('/prefetch-default'); | ||
}); | ||
|
||
test('data-astro-prefetch="false" should not prefetch', async ({ page, astro }) => { | ||
await page.goto(astro.resolveUrl('/')); | ||
expect(reqUrls).not.toContainEqual('/prefetch-false'); | ||
}); | ||
|
||
test('data-astro-prefetch="tap" should prefetch on tap', async ({ page, astro }) => { | ||
await page.goto(astro.resolveUrl('/')); | ||
expect(reqUrls).not.toContainEqual('/prefetch-tap'); | ||
await Promise.all([ | ||
page.waitForEvent('request'), // wait prefetch request | ||
page.locator('#prefetch-tap').click(), | ||
]); | ||
expect(reqUrls).toContainEqual('/prefetch-tap'); | ||
}); | ||
|
||
test('data-astro-prefetch="hover" should prefetch on hover', async ({ page, astro }) => { | ||
await page.goto(astro.resolveUrl('/')); | ||
expect(reqUrls).not.toContainEqual('/prefetch-hover'); | ||
await Promise.all([ | ||
page.waitForEvent('request'), // wait prefetch request | ||
page.locator('#prefetch-hover').hover(), | ||
]); | ||
expect(reqUrls).toContainEqual('/prefetch-hover'); | ||
}); | ||
|
||
test('data-astro-prefetch="viewport" should prefetch on viewport', async ({ page, astro }) => { | ||
await page.goto(astro.resolveUrl('/')); | ||
expect(reqUrls).not.toContainEqual('/prefetch-viewport'); | ||
// Scroll down to show the element | ||
await Promise.all([ | ||
page.waitForEvent('request'), // wait prefetch request | ||
page.locator('#prefetch-viewport').scrollIntoViewIfNeeded(), | ||
]); | ||
expect(reqUrls).toContainEqual('/prefetch-viewport'); | ||
expect(page.locator('link[rel="prefetch"][href$="/prefetch-viewport"]')).toBeDefined(); | ||
}); | ||
|
||
test('manual prefetch() works once', async ({ page, astro }) => { | ||
await page.goto(astro.resolveUrl('/')); | ||
expect(reqUrls).not.toContainEqual('/prefetch-manual'); | ||
await Promise.all([ | ||
page.waitForEvent('request'), // wait prefetch request | ||
page.locator('#prefetch-manual').click(), | ||
]); | ||
expect(reqUrls).toContainEqual('/prefetch-manual'); | ||
expect(page.locator('link[rel="prefetch"][href$="/prefetch-manual"]')).toBeDefined(); | ||
|
||
// prefetch again should have no effect | ||
await page.locator('#prefetch-manual').click(); | ||
expect(reqUrls.filter((u) => u.includes('/prefetch-manual')).length).toEqual(1); | ||
}); | ||
}); | ||
|
||
test.describe("Prefetch (prefetchAll: true, defaultStrategy: 'tap')", () => { | ||
let devServer; | ||
/** @type {string[]} */ | ||
const reqUrls = []; | ||
|
||
test.beforeAll(async ({ astro }) => { | ||
devServer = await astro.startDevServer({ | ||
prefetch: { | ||
prefetchAll: true, | ||
defaultStrategy: 'tap', | ||
}, | ||
}); | ||
}); | ||
|
||
test.beforeEach(async ({ page }) => { | ||
page.on('request', (req) => { | ||
reqUrls.push(new URL(req.url()).pathname); | ||
}); | ||
}); | ||
|
||
test.afterEach(() => { | ||
reqUrls.length = 0; | ||
}); | ||
|
||
test.afterAll(async () => { | ||
await devServer.stop(); | ||
}); | ||
|
||
test('Link without data-astro-prefetch should prefetch', async ({ page, astro }) => { | ||
await page.goto(astro.resolveUrl('/')); | ||
expect(reqUrls).not.toContainEqual('/prefetch-default'); | ||
await Promise.all([ | ||
page.waitForEvent('request'), // wait prefetch request | ||
page.locator('#prefetch-default').click(), | ||
]); | ||
expect(reqUrls).toContainEqual('/prefetch-default'); | ||
}); | ||
|
||
test('data-astro-prefetch="false" should not prefetch', async ({ page, astro }) => { | ||
await page.goto(astro.resolveUrl('/')); | ||
expect(reqUrls).not.toContainEqual('/prefetch-false'); | ||
}); | ||
|
||
test('data-astro-prefetch="tap" should prefetch on tap', async ({ page, astro }) => { | ||
await page.goto(astro.resolveUrl('/')); | ||
expect(reqUrls).not.toContainEqual('/prefetch-tap'); | ||
await Promise.all([ | ||
page.waitForEvent('request'), // wait prefetch request | ||
page.locator('#prefetch-tap').click(), | ||
]); | ||
expect(reqUrls).toContainEqual('/prefetch-tap'); | ||
}); | ||
|
||
test('data-astro-prefetch="hover" should prefetch on hover', async ({ page, astro }) => { | ||
await page.goto(astro.resolveUrl('/')); | ||
expect(reqUrls).not.toContainEqual('/prefetch-hover'); | ||
await Promise.all([ | ||
page.waitForEvent('request'), // wait prefetch request | ||
page.locator('#prefetch-hover').hover(), | ||
]); | ||
expect(reqUrls).toContainEqual('/prefetch-hover'); | ||
}); | ||
|
||
test('data-astro-prefetch="viewport" should prefetch on viewport', async ({ page, astro }) => { | ||
await page.goto(astro.resolveUrl('/')); | ||
expect(reqUrls).not.toContainEqual('/prefetch-viewport'); | ||
// Scroll down to show the element | ||
await Promise.all([ | ||
page.waitForEvent('request'), // wait prefetch request | ||
page.locator('#prefetch-viewport').scrollIntoViewIfNeeded(), | ||
]); | ||
expect(reqUrls).toContainEqual('/prefetch-viewport'); | ||
expect(page.locator('link[rel="prefetch"][href$="/prefetch-viewport"]')).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.