Edgio
Edgio

Predictive Prefetch

Edgio allows you to speed up the user’s browsing experience by prefetching pages and API calls that they are likely to need.

Traffic Shielding

You might think that prefetching will put significant additional load on the infrastructure hosting your APIs. That’s actually not the case! Edgio only serves prefetch requests from the edge cache. It will never make a request to the origin if a prefetch request cannot be served from the edge cache, so your servers will never see an increased load.

Service Worker

To enable prefetching, your site’s service worker needs to use the @layer0/prefetch library’s Prefetcher class. If your site doesn’t currently have a service worker, one can easily be created using Google’s Workbox.

Here’s an example service worker based on Workbox:

JavaScript
1import { skipWaiting, clientsClaim } from 'workbox-core'
2import { precacheAndRoute } from 'workbox-precaching'
3import { Prefetcher } from '@layer0/prefetch/sw'
4
5skipWaiting()
6clientsClaim()
7precacheAndRoute(self.__WB_MANIFEST || [])
8
9new Prefetcher().route()

Window

Once you’ve created a service worker, code running in the browser window needs to register the service worker before prefetching can begin. How you do this depends on the front-end framework that you use.

If you’re not using a front-end framework, you can use the install function from @layer0/prefetch to install the service worker. Here’s an example:

JavaScript
1import install from '@layer0/prefetch/window/install'
2
3install()

Prefetching a URL

To prefetch a URL, call the prefetch function from @layer0/prefetch/window:

JavaScript
1import { prefetch } from '@layer0/prefetch/window'
2
3prefetch('/some/url')

Prefetch requests are given the lowest priority. This ensures that they do not block more critical requests like API calls, images, scripts, and navigation.

The cache-manifest.js File

This file is generated at runtime and is used by the Prefetcher class from @layer0/prefetch to add routes to the service worker. The routes ensure that custom cache keys and the serviceWorkerSeconds properties from the cache() settings in your router are propagated to the service worker.

For more information on Prefetcher, serviceWorkderSeconds, and cache(), see Class Prefetcher.

React

The @layer0/react package provides a Prefetch component that you can wrap around any link to prefetch the link when it becomes visible in the viewport:

JavaScript
1import { Prefetch } from '@layer0/react'
2
3function MyComponent() {
4 return (
5 <Prefetch url="/api/for/some/page">
6 <a href="/some/page">Some Page</a>
7 </Prefetch>
8 )
9}

By default, Prefetch will fetch and cache the URL in the link’s href attribute. If you have a single page app, you most likely want to prefetch an API call for the page rather than the page’s HTML. The example above shows you how to set the url property to control which URL is prefetched.

If you’re using Next.js, the Prefetch component assumes you’re using getServerSideProps and will prefetch the corresponding URL unless your specify a url prop. The Prefetch component should be placed between Next’s <Link> and the <a> element:

JavaScript
1import Link from 'next/link'
2import { Prefetch } from '@layer0/react'
3
4export default function ProductListingPage() {
5 return (
6 <Link as="/p/1" href="/p/[productId]">
7 <Prefetch>
8 <a>Some Page</a>
9 </Prefetch>
10 </Link>
11 )
12}
13
14export function getServerSideProps(context) {
15 return {
16 props: {}, // will be passed to the page component as props
17 }
18}

If you need to prefetch a different url, you can do so using the url prop:

JavaScript
1<Link as="/p/1" href="/p/[productId]">
2 <Prefetch url="/some/url/to/prefetch">
3 <a>Some Page</a>
4 </Prefetch>
5</Link>

Vue

The @layer0/vue package provides a Prefetch component that you can wrap around any link to prefetch the link when it becomes visible in the viewport:

JSX
1<template>
2 <Prefetch v-bind:url="/api/for/some/page">
3 <router-link v-bind:to="/some/page">Some page</router-link>
4 </Prefetch>
5</template>
6
7<script>
8 import Prefetch from '@layer0/vue/Prefetch'
9 export default {
10 components: {
11 Prefetch,
12 },
13 }
14</script>

By default Prefetch will fetch and cache the URL in the link’s to attribute (for both router-link and nuxt-link). If you have a single page app, you most likely want to prefetch an API call for the page rather than the page’s HTML. The example above shows you how to set the url property to control which URL is prefetched.

Deep Fetching

By default, prefetching only fetches the JSON API data or HTML document for a prefetched page. In order to achieve truly instant page transitions, all of the page’s assets above the fold need to be prefetched as well. These typically include images, CSS, and JavaScript. This is where “deep fetching” comes in. Deep fetching parses the prefetched page and then fetches the important assets of the prefetched page that you specify.

To add deep fetching to your project, add the DeepFetchPlugin to your service worker. The DeepFetchPlugin is then configured with an array of selectors that describe which assets need to be prefetched:

JavaScript
1import { Prefetcher } from '@layer0/prefetch/sw'
2import DeepFetchPlugin from '@layer0/prefetch/sw/DeepFetchPlugin'
3
4new Prefetcher({
5 plugins: [
6 new DeepFetchPlugin([
7 {
8 /* Deep fetching configuration objects go here */
9 },
10 ]),
11 ],
12})

The DeepFetchPlugin can parse both HTML and JSON documents to extract the page assets that must be deep fetched. For Edgio projects that are headless (i.e. the front end communicates with the backend through an API), you’ll typically use the JSON option. However if the backend and front-end endpoints are communicating using HTML responses then you’ll want to use the HTML option. Note that you can mix both HTML and JSON configuration objects in the an array passed to the DeepFetchPlugin.

Deep fetching URLs in JSON responses

For JSON responses, you’ll pass the DeepFetchPlugin an array of DeepFetchJsonConfig interface objects. These DeepFetchJsonConfig objects describe the asset URLs in the JSON response that should be prefetched. For example, the snippet below finds product images to deep fetch for a category page response:

JavaScript
1new DeepFetchPlugin([
2 // parses the category API response to deep fetch the product images:
3 {
4 jsonQuery: 'Bundles.[**].Products:products(Product).MediumImageFile',
5 jsonQueryOptions: {
6 locals: {
7 // filters out null products:
8 products: input => input.filter(prod => prod),
9 },
10 },
11 maxMatches: 10,
12 as: 'image',
13 },
14])

The jsonQuery syntax is provided by the json-query library. You can test your JSON queries using their JSON-query Tester Sandbox.

Deep Fetching for HTML documents

To deep fetch HTML documents, pass the plugin objects that match the DeepFetchHtmlConfig interface and describe which HTML elements need to be prefetched via CSS selectors.

For example, imagine you’re configuring prefetching for a product page and you want to ensure the main product image is prefetched so that it appears immediately when the page loads. If the main product image is displayed with an HTML img element with a CSS class called product-featured-media, it can be prefetched by adding the following to the DeepFetchPlugin:

JavaScript
1import { Prefetcher } from '@layer0/prefetch/sw'
2import DeepFetchPlugin from '@layer0/prefetch/sw/DeepFetchPlugin'
3
4new Prefetcher({
5 plugins: [
6 new DeepFetchPlugin([
7 {
8 selector: 'img.product-featured-media', // CSS selector syntax - just like you would use with document.querySelector()
9 maxMatches: 1, // limits the number of matched elements to prefetch to 1 per page
10 attribute: 'src', // the attribute holding the URL to prefetching
11 as: 'image', // the type of asset being prefetched
12 },
13 ]),
14 ],
15})

Computing the URL to be prefetched

In the example above the img element’s src attribute contains URL that needs to be prefetched. Sometimes finding the URL to prefetch is not so straightforward. For example, apps sometimes use JavaScript to compute the URL for responsive images based on the user’s device size. In such cases you can provide a callback function which will be passed all matching elements and decide what URLs to prefetch. Here is an example:

TypeScript
1import { Prefetcher, prefetch } from '@layer0/prefetch/sw'
2import DeepFetchPlugin, {
3 DeepFetchCallbackParam,
4} from '@layer0/prefetch/sw/DeepFetchPlugin'
5
6new Prefetcher({
7 plugins: [
8 new DeepFetchPlugin([
9 {
10 selector: 'img.grid-view-item__image',
11 maxMatches: 4,
12 as: 'image',
13 callback: deepFetchResponsiveImages,
14 },
15 ]),
16 ],
17})
18
19function deepFetchResponsiveImages({ $el, el, $ }: DeepFetchCallbackParam) {
20 const urlTemplate = $el.attr('data-src')
21 const dataWidths = $el.attr('data-widths')
22
23 if (dataWidths && urlTemplate) {
24 const widths = JSON.parse(dataWidths)
25
26 for (let width of widths.slice(0, 2)) {
27 const url = urlTemplate?.replace(/\{width\}/, width)
28 prefetch(url, 'image')
29 }
30 }
31}

Using Edgio for Prefetching Only

If you have an existing site already in production, it is possible to prefetch from Edgio while still serving the site from the existing CDN.

To achieve this:

  1. Create a new Edgio app using npm create layer0-app.
  2. Use your site’s hostname as the origin site.
  3. Once the app is created, configure your routes file to cache the URLs you want to prefetch.
  4. Deploy your Edgio app.
  5. (Optional) Give it a custom domain by creating a production environment, assigning a custom domain, and uploading an SSL certificate.
  6. In your service worker source, use the cacheHost option when configuring the Prefetcher. For example:
JavaScript
1import { skipWaiting, clientsClaim } from 'workbox-core'
2import { Prefetcher } from '@layer0/prefetch/sw'
3
4skipWaiting()
5clientsClaim()
6
7new Prefetcher({
8 cacheHost: 'your.layer0.domain.here.com', // specify the domain name for your Edgio app here
9})
  1. Serve the service worker from your site’s origin domain. This is critical because service workers can only intercept fetch calls from apps served from the same origin as the service worker.
  2. Add a script to your app’s source to install the service worker on each page. Here’s an example:
JavaScript
1import { install, prefetch } from '@layer0/prefetch/window'
2
3document.addEventListener('DOMContentLoaded', function() {
4 install({
5 // Since there is no direct traffic to Edgio, the cache will only be populated from prefetch
6 // requests, so we need to serve some of the prefetch requests even when they are not cached.
7 // Here we choose to do so with 20% of the prefetch requests.
8 forcePrefetchRatio: 0.2,
9
10 // You can change this if you need to serve the service worker on a different path
11 serviceWorkerPath: '/service-worker.js',
12
13 // If you don't have links specified with a `<a>` tags with `href` attributes, you can also
14 // specify watchers to prefetch when other elements are added to the page:
15 watch: [
16 {
17 selector: 'div.product-tile',
18 callback: el => {
19 const productId = el.getAttribute('data-product-id')
20 const catId = document.getElementById('cat-listing').getAttribute('data-category-id')
21 prefetch(`/api/${catId}/${productId}`, 'fetch')
22 },
23 },
24 ],
25 })
26})

GraphQL

Edgio also enables caching and prefetching of GraphQL requests via a middleware for Apollo. To enable prefetching of GraphQL queries in both the edge and the service worker:

  1. Ensure that your GraphQL API is configured to accept GET requests. The Apollo client uses POST requests by default, but the Apollo server automatically accepts both GETs and POSTs. We use GETs instead of POSTs for two reasons:

    1. So that the URLs are sufficiently unique cache keys
    2. Browser cache APIs only support caching GETs
  2. Add @layer0/apollo to your project:

Bash
1npm i --save @layer0/apollo
  1. Add your GraphQL API as a backend to layer0.config.js. For example:
JavaScript
1// layer0.config.js
2
3module.exports = {
4 backends: {
5 graphql: {
6 domainOrIp: 'graphql.my-site.com',
7 hostHeader: 'graphql.my-site.com',
8 },
9 },
10}
  1. Add a GET route for the GraphQL endpoint to your router:
JavaScript
1const { Router, CustomCacheKey } = require('@layer0/core/router')
2const { decompressRequest } = require('@layer0/apollo')
3
4module.exports = new Router()
5 .post('/graphql', ({ proxy }) => {
6 proxy('graphql') // forward posts requests to apollo unaltered
7 })
8 .get('/graphql', ({ cache, removeUpstreamResponseHeader, proxy }) => {
9 cache({
10 edge: {
11 maxAgeSeconds: 60 * 60 * 24,
12 staleWhileRevalidateSeconds: 60 * 60,
13 },
14 browser: {
15 maxAgeSeconds: 0,
16 serviceWorkerSeconds: 60 * 60 * 24,
17 },
18 })
19
20 // Some APIs, like Shopify, attempt to establish a session by setting a cookie. Edgio will
21 // not cache responses with a set-cookie header, so we remove it before attempting to write
22 // the response to the cache
23 removeUpstreamResponseHeader('set-cookie')
24
25 // Proxy the request to the "graphql" backend configured in layer0.config.js
26 // Here we use decompressRequest to decompress and extract the GraphQL query from the URL's query string
27 // and convert the GET to a POST when connecting to the GraphQL server.
28 proxy('graphql', { transformRequest: decompressRequest })
29 })
  1. Configure your Apollo client to use a custom link from @layer0/apollo’s createHttpLink function. For example:
JavaScript
1import { createHttpLink } from '@layer0/apollo'
2
3export default () => ({
4 defaultHttpLink: false,
5 link: createHttpLink({
6 credentials: 'omit',
7 uri:
8 typeof window === 'undefined' // Use a relative URL when running in the browser so that GraphQL requests are fetched via Edgio's edge cache.
9 ? process.env.GQL_ENDPOINT
10 : '/graphql',
11 headers: {
12 'X-Shopify-Storefront-Access-Token': process.env.GQL_ACCESS_TOKEN,
13 },
14 }),
15})

The createHttpLink function accepts all of the options documented here and automatically uses GET requests for all queries so that they can be cached at the edge and prefetched by the service worker.

  1. Use createApolloURL(client, query, variables) to create the URL to prefetch:
JavaScript
1import { Prefetch } from '@layer0/react'
2import { createApolloURL } from '@layer0/apollo'
3import productById from '../apollo/queries/productById.gql'
4
5function MyProductLink({ product }) {
6 return (
7 <Prefetch url={createApolloURL(this.$apollo, productById, { id: product.id })}>
8 <a href={product.url}>{product.name}</a>
9 </Prefetch>
10 )
11}

You can test that everything is running locally by running your project with:

Bash
10 dev --cache

Advantages over Apollo’s prefetch functionality

Apollo provides it’s own ability to prefetch data. Prefetching using the method described above has a number of advantages:

  • It minimizes the amount of data that needs to be transmitted in response to the initial request, making the page faster.
  • Prefetched data is held in the service worker cache so it can be used in the event that the user navigates away from your website and returns later.
  • Data is prefetched with low priority so that prefetch requests will not block other more important requests like navigation and images.

Reducing 412s

By default, Edgio will only serve prefetch requests from the edge cache. If a request cannot be served from the cache, a 412 status is returned. This protects your origin servers from additional traffic associated with prefetching. If you’re seeing a surprisingly high number of 412s in your logs:

  1. Ensure that the URLs you’re prefetching match exactly those that are fetched during page navigation. Prefetch URLs will have ?layer0_prefetch=1 whereas the URLs associated with page navigation won’t. That’s okay. The layer0_* query parameters are automatically excluded from the cache key. Just ensure that there are no other differences.
  2. Ensure that cache settings have stale-while-revalidate enabled. For example:
JavaScript
1router.get('/p/:productId', ({ cache }) => {
2 cache({
3 edge: {
4 maxAgeSeconds: 60 * 60,
5 staleWhileRevalidateSeconds: 60 * 60 * 24, // this way stale items can still be prefetched
6 },
7 })
8})
  1. Consider increasing edge.maxAgeSeconds. The shorter the cache time to live is, the more prefetches will fail.
  2. Set the includeCacheMisses: true prefetch install option. This should be used with caution and is not recommended for use in Production because it will significantly increase requests to your origin or API servers.