Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sub Requests triggered by DatasetReference are not cacheable correctly #212

Open
cbou opened this issue Sep 5, 2024 · 1 comment
Open

Comments

@cbou
Copy link

cbou commented Sep 5, 2024

In headless situation, caching is very important, especially for requests triggered to the backend.

In my project, I use fetchByFilter to get some objects.
To prevent making too much request to the backend I rely on the cache mechanism of NextJS: https://nextjs.org/docs/app/building-your-application/data-fetching
I pass a fetchOptions object with the information about the caching to fetchByFilter. e.g.:

    fetchOptions: {
      next: {
        tags: ["my-tag"],
        revalidate: 3600,
      },
    },

NextJS will cache the response of the fetch for one hour. The next calls will be served from the cache instead of from the backend. It works well.

If the returned objects contains some DatasetReference. Sub requests will be triggered internally by the fsxa-api library.
However the fetchOptions param is lost and not passed by the original fetchByFilter call.

This makes caching of sub request impossible.

I was able to check that by adding some extra logging.

FSXARemoteApi.js

    FSXARemoteApi.prototype.fetchByFilter = function (_a, mapper) {
        console.log('Fetch by filter');
        console.log(_a.fetchOptions);

The produced log:

$ npx ts-node index-countries.ts
Fetch by filter
{ next: { tags: [ 'my-tag' ], revalidate: 3600 } }
Fetch by filter
undefined
Fetch by filter
undefined
Fetch by filter
undefined
Fetch by filter
undefined
Fetch by filter

Is there a way to make sure the fetchOptions is pass to sub fetchByFilter calls?

@jodeleit-es
Copy link
Collaborator

There is currently no way to get fetchOptions propagated to subsequent requests. And I doubt that it would be a good idea to have these propagated by default.
You can e.g. opt-out of automatic reference resolving (by setting the depth to zero) and do then the resolving yourself (including your desired options).

But we may accept a patch as long as it is non-breaking (ie with an opt-in flag).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants