Gatsby: HTML5 Download Link

Created on 30 Nov 2017  路  7Comments  路  Source: gatsbyjs/gatsby

I'm trying to do something fairly simple - offer a file for download, but am finding much difficulty in the process. I'm specifically wanting to offer a yaml and a pdf for download. Should this be done using the static folder? Thank you for the assistance!

Specifically looking to do something like:
<a href="/src/pages/category/_fields.yaml" download="fields.yaml">Download</a>

Most helpful comment

Neat project! Glad that importing is working well for you. You might have some trouble turning an imported *.yaml file into an downloadable asset (just a guess, not proven).

From the repo, I noticed that your static folder is not at the project root. It has to live at root level for Gatsby: /static/* will end up at /public/*. For instance, if you were to drop your favicon.ico in /static/, and then run gatsby build, it would end up in /public/favicon.ico.

I hope that helps! I'm definitely going to star that repo.

All 7 comments

Your /src/pages/category folder won't exist in your production site (after you run gatsby build), so you can use the static folder like you suggested. If you placed the file in /static/documents/, your href would be /documents/_fields.yaml.

Let me know how it works out for you! :-)

Thanks, @benforshey, that makes a lot of sense. I had tried using the static folder, but I'm discovering the problem is deeper - the contents of my static and img folders aren't actually getting copied into /public when running dev or building. I'm assuming something is wonky in my gatsby configuration causing this issue.

Sure thing, @beardedfinch. If you feel like opening up a public repo, I'd be happy to take a look at your code.

You probably already know this, but Gatsby generally recommends keeping things within the module system鈥攊mporting your images if at all possible instead of dropping them into a static image folder. You can read more about why in Adding Images, Fonts, and Files.

Thanks again, @benforshey. This is likely the result of growing pains from updating gatsby from 0.10 to 1.9.

The site is open source, so feel free to take a look and let me know if you see where something went wrong: repo.

@benforshey importing seems to be working well, so I'll move forward with that and convert the other site resources to do the same. Thank you again for the assistance.

Neat project! Glad that importing is working well for you. You might have some trouble turning an imported *.yaml file into an downloadable asset (just a guess, not proven).

From the repo, I noticed that your static folder is not at the project root. It has to live at root level for Gatsby: /static/* will end up at /public/*. For instance, if you were to drop your favicon.ico in /static/, and then run gatsby build, it would end up in /public/favicon.ico.

I hope that helps! I'm definitely going to star that repo.

Thanks for the tip on the static folder, @benforshey, that's a big help. You're right about the yaml file - it just gets read in as an object - I'll find a workaround for that or just download a different file type.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brandonmp picture brandonmp  路  3Comments

theduke picture theduke  路  3Comments

dustinhorton picture dustinhorton  路  3Comments

Oppenheimer1 picture Oppenheimer1  路  3Comments

kalinchernev picture kalinchernev  路  3Comments