Build Plugin get preview URL onPostBuild

Hi

I’m working on a build plugin that generates a PDF from the provided URL (eg. my-site.netlify.app) and then saves it to publish directory. I managed to save it to the publish dir onPostBuild, but the PDF is generated from the previous build. So I tried to do it onSuccessevent, but then the file is not accessible. I’m not sure why, but it makes sense. So I’m looking for 2 solutions, either one would be fine.

  1. Can I access a preview URL with onPostBuild event already? I need to serve Puppetteer an http link, not a relative path.
  2. Is possible to save a file to publish dist with onSuccess event so that it is accessible?

repo: GitHub - martinjagodic/netlify-plugin-generate-pdf: Generate a PDF from HTML at the provided URL and save it to publish directory.

Thanks for your help!

Hi @martinjagodic,

Netlify sites are only deployed once per build. This happens between onPostBuild and onSuccess.

As a consequence:

  1. Accessing a preview URL during onPostBuild will show the previous build, since the current build has not been deployed yet.
  2. Any files saved to the publish directory during onSuccess will not be deployed, since the current build has already been deployed.

It seems to me what you’re trying to achieve might not be possible. However, there might be alternative solutions:

  • Instead of a Build plugin, could you use a Netlify Function in a second site? It could generate the PDF, then save it to its publish directory.
  • Alternatively, could the Build plugin run the site locally in order to generate the PDF? Considering how variable site setups are, this is most likely to only work for a specific framework/setup in mind.

Hi @ehmicky ,

thanks for the explanation, I was afraid that was the case.

First, I used a Netlify function to achieve this, but a plugin seems more appropriate and reusable.

I use Puppeteer (headless chromium) to generate PDFs, which only accepts HTTP addresses, so the local build would not work.

Another solution would be to store generated files to the cloud, but I really tried to avoid that.

For now, I will keep it at that. If it uses a previous build, that is fine for my needs.

2 Likes