Add setting to return 301 or 302 response for images

Created on 26 September 2024, 7 months ago

Problem/Motivation

Permanent 301 Redirects are better for SEO than temporary 302 redirects. Add a setting in config to allow users to return the files using 301 redirects instead of default 302.

Steps to reproduce

Request a file on the site which is hosted in S3. For example, curl -I https://mydomain.com/s3/files/styles/article_banner_small/public/2024-09/asdf.webp?itok=UR4xYLcd

Curl responds with a 302 redirect to the file

Proposed resolution

Configure S3FS to respond with a 301 redirect instead.

Remaining tasks

  • Add config option for http status code response
  • Update TrustedRedirectResponse() call with the status code selected from config.
  • Test

User interface changes

Add new config field to settings form

API changes

None

Data model changes

New status code field to be added to config/install and config/schema files

✨ Feature request
Status

Active

Version

3.0

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada sagesolutions

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @sagesolutions
  • πŸ‡ΊπŸ‡ΈUnited States cmlara

    This is less of an SEO consideration and more of a technical deployment issue. RFC 7231 is relevant here.

    6.4.2. 301 Moved Permanently

    6.4.3. 302 Found

    I will note that s3fs proactively takes action to purge pages from the cache that use the image style route upon an image derivative being generated to reduce the number of locations that these links exist (primarily as a sever load mitigation though it is relevant here as it also mitigates search engines seeing style links).

    A key issue is that an image style derivative may be deleted at any time. A client(browser) may need to load the generation URL in the future to obtain a working image. If a browser observes a 301 and the image style cache is than purged the client upon reloading the page may observe the image style request link and incorrectly (as it relates to s3fs operations) visit the S3 bucket directly leading to a 404 with no way for the user to generate the image.

    I'm inclined to won't-fix this as we have a necessary technical reason to use 301's unless reason can be provided to justify ignoring the RFC's for web responses that resolve the fault cases related to 301's.

    A site owner can use a cache warming module if they wish to avoid redirects. If the image derivative already exists at page render time s3fs will generate the link directly to the object in the s3 bucket.

  • πŸ‡¨πŸ‡¦Canada sagesolutions

    Hi @cmlara,

    Thanks for the thorough response.

    I see that the image urls on my site are actually pointing to the cdn domain and loading the file directly from the cdn s3 bucket; not using the /s3 path. I'm okay with closing this ticket.

  • πŸ‡ΊπŸ‡ΈUnited States cmlara

    Glad existing features are working for you!

    Closing the issue.

Production build 0.71.5 2024