@astrojs/prefetch

中文文档
0.4.1发布于 2023/10/05MITTypeScriptES Module构建来源已验证近 30 天下载 7730

Prefetch page navigations in your Astro site

此软件包已被标记为弃用

@astrojs/prefetch is deprecated in favor of the builtin prefetch option. Please see the migration guide for more information: https://docs.astro.build/en/guides/prefetch/#migrating-from-astrojsprefetch

原始使用文档
文档目录12 节
  1. Why Prefetch?
  2. Installation
  3. Quick Install
  4. Manual Install
  5. Usage
  6. Configuration
  7. config.selector
  8. config.intentSelector
  9. config.throttle
  10. Troubleshooting
  11. Contributing
  12. Changelog

@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!

In addition, you can add rel="prefetch-intent" to any <a /> links on your page to prefetch them only when they are hovered over, touched, or focused. This is especially useful to conserve data usage when viewing your site.

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.intentSelector

By default, the prefetch script also searches the page for any links that include a rel="prefetch-intent" attribute, ex: <a rel="prefetch-intent" />. This behavior can be changed in your astro.config.* file to use a custom query selector when finding prefetch-intent 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` or `/coupons`
      intentSelector: ["a[href^='/products']", "a[href^='/coupons']"],

      // Use a string to prefetch a single selector
      // intentSelector: "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.

版本与发布记录

@astrojs/prefetch 共发布 31 个版本,首次发布于 2022/06/28;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.4.1latest2023/10/053 年前
0.4.02023/08/303 年前
0.4.0-rc.12023/08/223 年前
0.4.0-beta.02023/08/033 年前
0.3.02023/07/113 年前
0.2.32023/05/213 年前

其他发布通道:next--rc → 0.0.0-rc-20220726043527,next--config-errors → 0.0.0-config-errors-20221101194733,next--cloudcannon-fix → 0.0.0-cloudcannon-fix-20230306211609,next--experimental-assets → 0.0.0-experimental-assets-20230307131344,next--vercel-json → 0.0.0-vercel-json-20230313201834,next--spa → 0.0.0-spa-20230519211519。

关于 @astrojs/prefetch 的常见问题

@astrojs/prefetch 是什么?

Prefetch page navigations in your Astro site注意:该包已被作者标记为弃用——@astrojs/prefetch is deprecated in favor of the builtin prefetch option. Please see the migration guide for more information: https://docs.astro.build/en/guides/prefetch/#migrating-from-astrojsprefetch

如何安装 @astrojs/prefetch?

在项目目录运行 npm install @astrojs/prefetch,或使用 pnpm add / yarn add / bun add。

@astrojs/prefetch 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@astrojs/prefetch 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@astrojs/prefetch 可以商用吗?

@astrojs/prefetch 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@astrojs/prefetch 的下载量有多少?

根据 npm 官方下载统计,@astrojs/prefetch 在 2026/10/07 – 2026/11/05 这 30 天内被下载 7,730 次,日均约 258 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@astrojs/prefetch 还在维护吗?

最新版本 0.4.1 发布于 2023/10/05(3 年前);至今共发布 31 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/06/28。作者已将其标记为弃用,新项目建议寻找替代方案。