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

How to cache JSON #739

Open
hiasl opened this issue Nov 25, 2024 · 3 comments
Open

How to cache JSON #739

hiasl opened this issue Nov 25, 2024 · 3 comments
Labels
question Further information is requested

Comments

@hiasl
Copy link

hiasl commented Nov 25, 2024

Support Request

Hi Ben,
we want to introduce a feature where we can render all HTML pages in a certain JSON format. This is realized using a custom route with the normal URL and ".json" at the end. We then generate the JSON with TWIG, set the correct mime type and deliver it. In our current setup Blitz however caches the page like this <whatever.json>/index.html and therefor it is not sent with a JSON mime type anymore.

I found this issue #443 where you wrote a fix, but in my case it has the same behaviour, although we are on a later version. I did not find anything in the docs pointing me in the right direction.

So my questions:

  • Is it possible to cache my .json requests
  • If yes, how can I enable it?

Thanks

Plugin Version

4.23.7

@hiasl hiasl added the question Further information is requested label Nov 25, 2024
@bencroker
Copy link
Collaborator

Can you send me a sample template so I can test locally?

@matuzo
Copy link

matuzo commented Nov 27, 2024

In our routes.php we have:

<?php
return [
  '<shareSlug:{slug}>.json' => ['template' => '_entry-json.twig']
];

In _entry-json.twig:

{% header "Content-Type: application/json; charset=utf-8" -%}

{
    "title": "this is just a test"
}

That should be enough to reproduce it. On the first visit you get a JSON file but on all consecutive visits it's HTML.

@bencroker
Copy link
Collaborator

It works as expected in my local tests. Can you please double check that you’ve enabled the cacheNonHtmlResponses config setting?

Keep in mind that if you’re using server rewrites, you may need to configure your web server to set the appropriate content type for URIs ending in .json.

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

No branches or pull requests

3 participants