Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Sapper export also follows <img src='...'/> and exports these #938

Closed
wants to merge 2 commits into from

Conversation

grjzwaan
Copy link

This PR is not related to any issue that I could find, I just wanted something like this and made a PR.

It might not be the cleanest way to solve this issue, or even desired, so I welcome feedback.

Current behaviour

Sapper export follows <a href='> links, but not <img src="catlolz.png"/>.

New behaviour

Sapper export follows <img src='catlolz.pong'/> links and exports the image.

Changes

  • Also follow the url in src attributes;
  • When fetching results, determine based on the mime to use r.text() or r.arrayBuffer();
  • Write files using a buffer (then writing the file works the same for text or arrayBuffer);
  • Added a test

Why

For myself I have a static website with blog posts in the following structure:

/blog/2019/11/01/index.md
/blog/2019/10/12/index.md
/blog/2019/10/12/catlolz.png
...

The index.md file refers to the image. I could put the image in the static folder and refer to it but the image belongs to this post. It seems nicer to also intercept requests for images in the export.

You do need an endpoint that serves the images, but this is also an advantage because now we can optimize those images dynamically (resize, compress, etc) by giving information from the front end.

Points of attention

You probably do not want to wildly grab all image links on your website. I think there was a filter in Sapper to exclude files from outside your own website, so that should apply. I didn't test this yet.

@aloker
Copy link
Contributor

aloker commented Feb 27, 2020

Sapper should also follow image links in <source srcset/> and <img srcset/>.

@aloker
Copy link
Contributor

aloker commented Feb 28, 2020

I took the liberty to implement a comparable solution myself, including srcset support (#1105)

@Conduitry
Copy link
Member

Closing in favor of #1398, which is built on this. I've included you as a co-authored-by on the commit. Thank you!

@Conduitry Conduitry closed this Aug 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants