Skip to content

feat: allow rules for ignoring queries #353

@remihuigen

Description

@remihuigen

🆒 Your use case

When configuring ogImage it would be great if there is somewhat more flexibility in how route queries are handled, or how cache keys are created.

Currently, any unique route query will create a new cache entry. But for most purposes, this not needed. (at least not in my case).

Let's say there is an og image cached for example.com/my-dynamic-page. When i request the route example.com/my-dynamic-page?foo=bar, the requested og image resource is

https://example.com/__og-image__/image/my-dynamic-page/og.png?foo=bar&_query=%7B%22foo%22:%22bar%22%7D

The existing cache is not hit, a new image will be rendered and cached thats identical to the original, except for the key it's stored under.

🆕 The solution you'd like

I'm aware that this might be exactly what you want, especially is the route query has an effect on the content for the og image. But for most of my projects, that is not the case.

I think the simplest solution would be to add a config option

ogImage: {
  runtimeCacheStorage: {
    // driver options
  },
  defaults: {
    cacheMaxAgeSeconds: 60 * 60 * 24 * 7,
  },
  ignoreQuery: true, 
  zeroRuntime: false
}

Which should have the effect that the asset from my example would be requested at
https://example.com/__og-image__/image/my-dynamic-page/og.png, regardless of what query was used.

As an alternative, you could provided a getKey handler option, so we'd be able to fully customise the cache keys

🔍 Alternatives you've considered

I'm currently using this workaround in my server middleware

import { parseURL } from 'ufo'

export default defineEventHandler(async (event) => {
  // Skip during prerendering
  if (import.meta.prerender) return

  const { pathname, search } = parseURL(event.path)

  // Check if ogImage with query in pathname. If so, redirect to path without query
  if (pathname.startsWith('/__og-image') && !!search) {
    await sendRedirect(event, pathname, 301)
    return
  }
})

ℹ️ Additional info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions