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

Automatically follow links and download content? #27

Open
kyrsquir opened this issue Mar 17, 2020 · 4 comments
Open

Automatically follow links and download content? #27

kyrsquir opened this issue Mar 17, 2020 · 4 comments

Comments

@kyrsquir
Copy link

kyrsquir commented Mar 17, 2020

I have a lot of links like /api/content/[content_id].[content_type] (images and pdfs in my case) which fetch binary files from the API. Currently this content is served dynamically along with generated static app. I want to automatically download it and save to, say, /static folder during the generation process.

Do I have to write a function for that which manually replicates the file structure in order to do that and then run it at some of the build hooks (and then proxying /api/content URLs into /static when serving), or is there at least a theoretical way to retrieve all these links during generation and make Node follow them, download and store the files mimicking the original url while rewriting the links in the process?

If what I want is theoretically possible, but cannot be implemented right now with some configuration, I might consider digging into this and possibly making a PR at some point in case the idea makes sense.

@pimlie
Copy link
Member

pimlie commented May 14, 2020

You could add a build:before or generate:before hook to download these files. Then in your component you could use a dynamic import statement, depending on further config you have, Webpack will then add all these files as static assets to your dist. See here for configuration options through magic comments.

@kyrsquir
Copy link
Author

kyrsquir commented May 15, 2020

You could add a build:before or generate:before hook to download these files.

Downloading the files in advance won't really work as there is neither a comprehensive list of files to download nor a good way to build such a list with exactly the needed files before the site is generated. But perhaps there is a way for generator to access the already built routes and retrieve a list of urls from it?

@pimlie
Copy link
Member

pimlie commented May 15, 2020

Can you add some example code that shows how and when you determine which content to download?

@kyrsquir
Copy link
Author

kyrsquir commented May 15, 2020

I have two dynamic urls, each used many times with different parameters:

data: ->
  src: "/api/content/cover/#{if @issue.feature then @issue.feature else @issue.year}/#{@issue.order_within_year}"

data: ->
  if isArticle
    url = "/api/content/pdf/#{@year}/#{@orderWithinYear}/#{@orderWithinIssue}/#{@orderWithinSection}"
  else
    url = "/api/content/pdf/#{@year}/#{@orderWithinYear}"
  url: url

In case that matters, they are being set to <img> and <a> tags, respectively.

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

2 participants