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

Intercepting <a download> Causes a Push Navigation #275

Open
absurdprofit opened this issue Aug 30, 2024 · 0 comments
Open

Intercepting <a download> Causes a Push Navigation #275

absurdprofit opened this issue Aug 30, 2024 · 0 comments

Comments

@absurdprofit
Copy link
Contributor

I was testing the use case of extending the download behaviour of anchor tags. For example we might want to extend it with the File System API:

<a href="src/assets/art.jpeg?fileType=jpeg" download="Cover Art" />
navigation.addEventListener("navigate", (e) => {
  if (e.downloadRequest) {
    e.intercept({ handler: async () => {
      const url = new URL(e.destination.url);
      const params = Object.fromEntries(url.searchParams.entries());
      const { fileType } = params; // additional metadata
      // get a file handle and create writable stream
      const fileHandle = await window.showSaveFilePicker({
        types: [FileTypes[fileType]],
        suggestedName: e.downloadRequest
      });
      const writable = await fileHandle.createWritable();
      // manually fetch the file and write to fileHandle
      const { body } = await fetch(url);
      body?.pipeTo(writable);
    }});
  }
});

However currently this snippet causes the download request to become a "real" push navigation and I end up at the destination URL, in this case src/assets/art.jpeg?fileType=jpeg.

Coming from the model of how unintercepted download requests work where they don't actually push on to the history stack this is a bit confusing and leaves me feeling like I'm missing something here.

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

1 participant