Comments (4)
I believe POST requests are supported if you pass some custom fetchOptions
(see example in 11ty docs; plus node-fetch
's fetch options docs):
const EleventyFetch = require("@11ty/eleventy-fetch");
module.exports = async function() {
const url = "https://httpbin.org/post#withData";
return EleventyFetch(url, {
duration: "1d",
type: "json",
fetchOptions: {
method: "POST",
body: JSON.stringify({name: "PeTeR"}),
}
});
};
But yes, if you are doing a lot of POSTing to the same endpoint, I think you'd need to use suitably unique URLs to bypass unwanted caching. Note the goofy #withData
slug in the url
above.
from eleventy-fetch.
@pdehaan's trick does work, but with the caveat that cache seems to work based off of the URL only.
So if you do multiple requests to the same URL but with different POST
values (i.e. fetching multiple pages of a paginated API endpoint, with the page / cursor ID passed in POST
), Fetch will treat these as the same request, and load the second one from cache.
We got around it by adding a hash that's unique to each request, i.e.;
async function fetchPage(pageId = 1) {
const data = await EleventyFetch(`https://www.example.com/api#${pageId}`, {
duration: '1d',
type: 'json',
fetchOptions: {
method: 'POST',
body: JSON.stringify({ page: pageId }),
},
});
//...
if (data.nextPage) {
await fetchPage(data.nextPage);
}
}
We found hash to be more reliable than a ?
query string, as the latter can lead to a rejected request by some particularly picky APIs.
But ultimately I think Eleventy Fetch should automatically include the contents of fetchOptions.body
as part of the hash generation, so this trick isn't necessary.
from eleventy-fetch.
Here is a workaround until a feature is implemented to solve this. Add a query string to the end of the url containing something that is unique, eg the request body:
await EleventyFetch(`https://api.github.com/graphql?__query=${body}`
from eleventy-fetch.
Related Issues (20)
- Saving asset when no .cache directory exists causes error HOT 1
- Change or disable "Caching: https://????" message HOT 1
- "ENOENT: no such file or directory" error if cache key has a slash in it HOT 1
- Cannot use 'in' operator to search for 'date' in 1
- `node-fetch` 2.6.1: security alert (CVE-2022-0235) HOT 2
- Rename to `eleventy-fetch` HOT 2
- Adapters for HTTP client under the hood HOT 1
- Mark @11ty/eleventy-cache-assets package as deprecated HOT 1
- Pass in a custom fetching function rather than a URL HOT 1
- Human readable cache file names HOT 2
- how to access response header info? HOT 8
- Using fetched/cached data in a web component HOT 1
- Reusing build time cache with serverless HOT 5
- Improve error handling by including response as the cause HOT 2
- Allow using GitHub access token or oauth to surpass api.github.com ratelimits HOT 6
- Add more debug output to AssetCache HOT 1
- Should `dryRun` skip reading from cache? HOT 1
- shorthash can generate the same identifier for different URLs HOT 2
- Is it possible to Cache an array of URLs? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eleventy-fetch.