How to determine the look-ahead request (Prefetch) from the browser

TL;DR Look-ahead requests (prefetch, Prefetch) may jump not only to css but also to other URLs. For a specific endpoint

--I want to know more accurately the number of GET requests caused by user actions ――Since it is a relatively heavy process, I do not want to process unnecessary requests as much as possible

If you have the above needs, you should take prefetch measures.

Background

While developing a Web system, I thought about the cause because an unintended GET request came into the log for the test server. After investigating, it seems that the browser may skip the GET request as a look-ahead (prefetch) process when entering something in the browser bar, and since a unique value is set in the header for that request, each browser The detection process was implemented to support the specifications of.

Header showing prefetch for each browser

Chrome(Chromium) Purpose: prefetch https://www.chromestatus.com/feature/6247959677108224 https://bugs.chromium.org/p/chromium/issues/detail?id=86175#c65

Firefox X-Moz: Prefetch https://developer.mozilla.org/ja/docs/Web/HTTP/Link_prefetching_FAQ

Safari X-Purpose: preview

Implementation example

import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;

public static boolean isPrefetchRequest(HttpServletRequest request) {
    return StringUtils.equalsAny(request.getHeader("purpose"), "Prefetch", "prefetch") ||
            StringUtils.equalsAny(request.getHeader("X-moz"), "Prefetch", "prefetch") ||
            StringUtils.equalsAny(request.getHeader("X-Purpose"), "Preview", "preview");
}

Supplement

――Since the specifications are not decided by a light examination, follow-up will be done on the assumption that changes will be made. --The information provided is as of the time of writing.

Recommended Posts

How to determine the look-ahead request (Prefetch) from the browser
How to determine the number of parallels
[Java] How to extract the file name from the path
How to migrate from JUnit4 to JUnit5
How to correctly check the local HTML file in the browser
How to transition from the [Swift5] app to the iPhone settings screen
Understand the rough flow from request to response in SpringWebMVC
[IOS] How to get the table name from AWS DynamoDB
How to create a form to select a date from the calendar
How to get the longest information from Twitter as of 12/12/2016
How to disable Set-Cookie from API on the front side
Learn how to customize the Navigation Bar from the sample app
[Docker] How to access the host from inside the container. http://host.docker.internal:
How to apply C code format from the command line
How to use the link_to method
How to use the include? method
How to push from Tarminal to GitHub
How to use the form_with method
How to find the average angle
How to use the wrapper class
[Rails] How to change the page title of the browser for each page
How to run a Kotlin Coroutine sample from the command line
How to add the delete function
The road from JavaScript to Java
[Java] How to convert from String to Path type and get the path
How to change from HTML to Haml
How to apply thymeleaf changes to the browser immediately with #Spring Boot + maven
How to return a value from Model to Controller using the [Swift5] protocol
[Spring Boot] I investigated how to implement post-processing of the received request.
How to run a GIF file from the Linux command line (Ubuntu)
Confirmation and refactoring of the flow from request to controller in [httpclient]
How to compare only the time with Rails (from what time to what time, something like)
How to get the setting value (property value) from the database in Spring Framework
[Java] How to use the File class
Investigate the replacement from Docker to Podman.
How to delete the wrong migration file
[Java] How to use the hasNext function
How to put out the error bundling
[Java] How to use the HashMap class
[Ruby] From the basics to the inject method
How to delete the migration file NO FILE
[Rails] How to use the map method
[IOS] How to get data from DynamoDB
Studying how to use the constructor (java)
How to call Swift 5.3 code from Objective-C
[Processing × Java] How to use the loop
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
[Swift] How to implement the countdown function
How to change the timezone on Ubuntu
[Flutter] How to use C / C ++ from Dart?
Ransack sort_link How to change the color!
[Processing × Java] How to use the class
Java: How to send values from Servlet to Servlet
How to sort the List of SelectItem
How to install the legacy version [Java]
How to find the tens and ones
How to pass the value to another screen
How to get the date in java
[Processing × Java] How to use the function
[Java] How to use the Calendar class