Coder Social home page Coder Social logo

Comments (4)

pdehaan avatar pdehaan commented on June 8, 2024 1

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.

groenroos avatar groenroos commented on June 8, 2024 1

@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.

petermekhaeil avatar petermekhaeil commented on June 8, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.