0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/integrations/prefetch
Subha Chanda 1c7eef308e
Updated according to new configuration (#5478)
* Updated according to new configuration

Astro imports the `defineConfig` function from `astro/config`. The `integrations` key needs to be passed into the `defineConfig` function, but it is not shown in the README. Updated the README according to the CLI example.

* update alpine

* update image

* update lit

* update mdx

* update preact

* update prefetch

* update react

* update sitemap

* update solid

* update svelte

* update tailwind

* update turbolinks

* update vue

* chore: add changeset

* update image

* update svelte readme

Co-authored-by: Nate Moore <nate@astro.build>
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
2023-01-26 13:03:00 -06:00
..
@types
src [ci] format 2022-11-01 12:44:07 +00:00
test Default preview host to localhost (#5753) 2023-01-09 12:14:07 -05:00
CHANGELOG.md [ci] release (#5262) 2022-11-03 09:38:29 -04:00
package.json Default preview host to localhost (#5753) 2023-01-09 12:14:07 -05:00
playwright.config.js Remove shamefully-hoist (#4842) 2022-09-28 23:13:33 +08:00
README.md Updated according to new configuration (#5478) 2023-01-26 13:03:00 -06:00
tsconfig.json

@astrojs/prefetch 🔗

Why Prefetch?

Page load times play a big role in usability and overall enjoyment of a site. This integration brings the benefits of near-instant page navigations to your multi-page application (MPA) by prefetching page links when they are visible on screen.

To further improve the experience, especially on similar pages, stylesheets are also prefetched along with the HTML. This is particularly useful when navigating between tabs on a static site, where most of the page's content and styles don't change.

Installation

Quick Install

The astro add command-line tool automates the installation for you. Run one of the following commands in a new terminal window. (If you aren't sure which package manager you're using, run the first command.) Then, follow the prompts, and type "y" in the terminal (meaning "yes") for each one.

# Using NPM
npx astro add prefetch
# Using Yarn
yarn astro add prefetch
# Using PNPM
pnpm astro add prefetch

If you run into any issues, feel free to report them to us on GitHub and try the manual installation steps below.

Manual Install

First, install the @astrojs/prefetch package using your package manager. If you're using npm or aren't sure, run this in the terminal:

npm install @astrojs/prefetch

Then, apply this integration to your astro.config.* file using the integrations property:

astro.config.mjs

import { defineConfig } from 'astro/config';
import prefetch from '@astrojs/prefetch';

export default defineConfig({
  // ...
  integrations: [prefetch()],
});

Usage

When you install the integration, the prefetch script is automatically added to every page in the project. Just add rel="prefetch" to any <a /> links on your page and you're ready to go!

Configuration

The Astro Prefetch integration handles which links on the site are prefetched and it has its own options. Change these in the astro.config.mjs file which is where your project's integration settings live.

config.selector

By default the prefetch script searches the page for any links that include a rel="prefetch" attribute, ex: <a rel="prefetch" /> or <a rel="nofollow prefetch" />. This behavior can be changed in your astro.config.* file to use a custom query selector when finding prefetch links.

astro.config.mjs

import { defineConfig } from 'astro/config';
import prefetch from '@astrojs/prefetch';

export default defineConfig({
  // ...
  integrations: [prefetch({
    // Only prefetch links with an href that begins with `/products`
    selector: "a[href^='/products']"
  })],
});

config.throttle

By default the prefetch script will only prefetch one link at a time. This behavior can be changed in your astro.config.* file to increase the limit for concurrent downloads.

astro.config.mjs

import { defineConfig } from 'astro/config';
import prefetch from '@astrojs/prefetch';

export default defineConfig({
  // ...
  integrations: [prefetch({
    // Allow up to three links to be prefetched concurrently
    throttle: 3
  })],
});

Troubleshooting

  • If your installation doesn't seem to be working, try restarting the dev server.
  • If a link doesn't seem to be prefetching, make sure that the link is pointing to a page on the same domain and matches the integration's selector option.

For help, check out the #support channel on Discord. Our friendly Support Squad members are here to help!

You can also check our Astro Integration Documentation for more on integrations.

Contributing

This package is maintained by Astro's Core team. You're welcome to submit an issue or PR!

Changelog

See CHANGELOG.md for a history of changes to this integration.