On This Page
If you've noticed an increase in 404 errors in your SvelteKit application logs, it might be due to the Chrome Privacy Preserving Prefetch Proxy bot. This bot prefetches content to speed up user navigation, but without proper handling, it can generate unwanted 404 errors. In this blog, we'll discuss how to address this issue and provide the necessary code to configure your SvelteKit server correctly.
Understanding Chrome Privacy Preserving Prefetch Proxy
Starting with Chrome 103 for Android, the Chrome browser includes a feature called the Privacy Preserving Prefetch Proxy. This feature helps improve the Largest Contentful Paint (LCP) by prefetching content anonymously via a proxy. However, without proper configuration, these prefetch requests can result in 404 errors if your server is not set up to handle them.
How the Prefetch Proxy Works
The prefetch proxy uses a secure communication channel to prefetch cross-origin content without exposing user information to the destination website until the user navigates there. Prefetch requests are uncredentialed, meaning they do not carry cookies or other local state. This can be beneficial for performance but requires handling specific request paths on your server, such as /.well-known/traffic-advice
.
Handling the well-known/traffic-advice Route
When the Chrome Privacy Preserving Prefetch Proxy bot prefetches content, it sends requests to the /.well-known/traffic-advice
path on your server.
To handle these prefetch requests properly with SvelteKit, you need to set up a route that responds to the specific URL used by the Chrome bot. This can be done using SvelteKit hooks, which allow you to intercept and modify incoming requests.
SvelteKit Hook for Prefetch Proxy
Below is the code to add to your src/hooks.server.ts
file to handle the /.well-known/traffic-advice
route and return the appropriate response:
Explanation of the Code
- Importing the Handle Type: The
Handle
type from@sveltejs/kit
is imported to type the handler function correctly. - Creating the Handler Function: The
wellknown_handler
function creates and returns a handle function. - Checking the Request URL: The handler checks if the incoming request URL ends with
/.well-known/traffic-advice
. - Returning the Prefetch Configuration: If the URL matches, it returns a JSON response with the prefetch configuration. This configuration informs the Chrome prefetch proxy to handle requests without generating 404 errors.
Adding the Hook to Your Project
To use this handler in your project, you'll need to ensure it is executed by your SvelteKit server. Update your src/hooks.server.ts
to include this handler:
Replace $lib/utils/hooks
with the actual path to your handler file.
In case you're using multiple handles, you can combine them using the sequence
function from @sveltejs/kit
:
By adding this handler, you can prevent unnecessary 404 errors caused by the Chrome Privacy Preserving Prefetch Proxy bot in your SvelteKit application.
This small configuration change can improve your server logs' clarity and ensure that your application handles prefetch requests correctly. Be sure to properly handle the /.well-known/traffic-advice
path to optimize your server's interaction with prefetch requests.
For more detailed information on this feature and its benefits, you can refer to the official Chrome blog post.