11ty/eleventy-fetch

"Caching an already local asset is not yet supported"

philapple opened this issue · 6 comments

I am trying to use this plugin to get Pinboard data via their API:

module.exports = async function() {
  let json = await Cache("https://${USERNAME}:${PASSWORD}@api.pinboard.in/v1/posts/all?format=json&tag=read", {
      duration: "1d",
      directory: ".cache",
      type: "json"
  });
  return json;
};

However I consistently get this error: "Caching an already local asset is not yet supported"

> Caching an already local asset is not yet supported.

`Error` was thrown:
    Error: Caching an already local asset is not yet supported.
        at save (/Users/filippo/playground/11ty/node_modules/@11ty/eleventy-cache-assets/eleventy-cache-assets.js:26:25)
        at queue.add (/Users/filippo/playground/11ty/node_modules/@11ty/eleventy-cache-assets/eleventy-cache-assets.js:44:25)
        at run (/Users/filippo/playground/11ty/node_modules/p-queue/dist/index.js:250:104)
        at PQueue._tryToStartAnother (/Users/filippo/playground/11ty/node_modules/p-queue/dist/index.js:198:38)
        at Promise (/Users/filippo/playground/11ty/node_modules/p-queue/dist/index.js:264:18)
        at new Promise (<anonymous>)
        at PQueue.add (/Users/filippo/playground/11ty/node_modules/p-queue/dist/index.js:245:16)
        at queueSave (/Users/filippo/playground/11ty/node_modules/@11ty/eleventy-cache-assets/eleventy-cache-assets.js:44:15)
        at module.exports (/Users/filippo/playground/11ty/_data/pinboard.js:10:22)
        at TemplateData.getDataValue (/Users/filippo/playground/11ty/node_modules/@11ty/eleventy/src/TemplateData.js:385:29)

I am a bit clueless to be honest, and I have run out of ideas on things to check that could be causing the issue. I feel there might be something obvious that I am missing.

@philapple Hmm, so I encountered this error when working on someones site last week. They had an instance of eleventy-cache-assets using let json = await Cache("url", {}); to access a local .json file. To my understanding the error made sense in this scenario as indeed the asset is local to the site and Caching an already local asset is not yet supported.

In your case, the asset is clearly attempting to return data with an external API request from Pinboard. Could you already have the Pinboard API data cached previously or available somewhere in the project locally causing the error to throw?

Lastly, must you have the query parameters in the Cache URL? ie /all?format=json&tag=read? If this isn't necessary to the resulting data maybe you should try Removing Query params using:

module.exports = async () => {
    let json = await CacheAsset("api-url", {
        duration: "1d",
        type: "json",
	removeUrlQueryParams: true
    });
}

Also, the directory key is by default .cache so you don't have to include that.

Note: I think this error is thrown when the request URL to Cache() is broken or undefined and the Promise is rejected

Received the same error today while working with the open graph api, I've noticed when the request URL to Cache("https://api...", {}); is null or broken and you attempt to use the global data returned from _data/stuff.js in a template with

module.exports = async () => {
    let json = await CacheAsset("some api url", {
	duration: "1d",
        type: "json"
    });
    return {
        title: json.titleKey,
        ...
    }
}
{{ stuff.title }}

the same error is thrown: > Caching an already local asset is not yet supported. Even when failing gracefully using a try/catch block, I get the same error.

Make sure the request URL is correct and aside from that, I can't seem to figure out what else causes the error. @zachleat

Also, could you log the <Promise> returned from the API request via console.log(json) to see what it shows? Without the try/catch block, the Promise could be pending or rejected.

As a note, returning json will return the promise from caching the API request via <Promise{}> which could then be used in a template as {{ test.someKey }} if the global JS file was named _data/test.js.

Looked into this a bit and without knowing the username and password used for the test URL I don’t think I’ll be able to figure it out. 😱

I do know that this error is thrown when the URL can’t be validated. So I’m guessing that the username or password fields there had some unencoded characters that caused the URL parser to fail. That said, I think it’s better practice to use the Authentication header instead? see https://serverfault.com/questions/371907/can-you-pass-user-pass-for-http-basic-authentication-in-url-parameters

This is an automated message to let you know that a helpful response was posted to your issue and for the health of the repository issue tracker the issue will be closed. This is to help alleviate issues hanging open waiting for a response from the original poster.

If the response works to solve your problem—great! But if you’re still having problems, do not let the issue’s closing deter you if you have additional questions! Post another comment and we will reopen the issue. Thanks!

Landed on this issue because I'm getting the same error message when building on Netlify (it's fine on local).

However, I think the original issue in this issue is failing because the url is using a normal string and not a template literals. The string isn't using a backtick (`) character and is instead is using double quotes and can't access the variables. cc: @philapple