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

feat: add prerender:route hook #213

Merged
merged 4 commits into from
May 10, 2022
Merged

feat: add prerender:route hook #213

merged 4 commits into from
May 10, 2022

Conversation

TotomInc
Copy link
Contributor

@TotomInc TotomInc commented May 9, 2022

πŸ”— Linked issue

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Add a new hook prerender:route that is called when a single page has been successfully prerendered.

This hook can be used externally to create specific logic tied to route generation, such as editing output of a route.

generateRoute now returns { contents, route, filePath } instead of nothing, in order to:

  • Access the saved file directly by its filePath.
  • Read the contents that have been generated.

I can see more potential for SSG (fully static).

A quick example usage could be to dynamically download and replace static assets, stored on an API server, that you don't want to use in production (parse HTML content, download image, replace image path with the one downloaded locally).

Please, let me know if this change is not intended for nitro.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

src/prerender.ts Outdated Show resolved Hide resolved
src/prerender.ts Outdated
nitro.logger.log(chalk.gray(` β”œβ”€ ${route} (${end - start}ms)`))
nitro.hooks.callHook('prerender:route', { route, contents, filePath })
} catch (error) {
nitro.logger.log(chalk.gray(` β”œβ”€ ${route} (${end - start}ms) ${error ? `(${error})` : ''}`))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also allow hook access to the errors rendered with error.

Copy link
Contributor Author

@TotomInc TotomInc May 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback.

I've made the return response of the prerender:route hook to be an object or an error. I also thought of introducing a hook to handle errors such as prerender:route:error. If you think it's cleaner to do this, I can make the change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking, that by using a single hook, we can allow:

  • Custom error (hooking to validate and modify context to extract an error being thrown)
  • Clear error (hooking to allow still prerendering some pages with errors)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I'm not sure I have understood correctly, but please let me know if my last changes seems good for you.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made few more refactors in the same PR to introduce this object I hope it gives some ideas for next steps :)

@pi0
Copy link
Member

pi0 commented May 9, 2022

Thanks! This also resolves #88

@pi0
Copy link
Member

pi0 commented May 10, 2022

❀️

@pi0 pi0 merged commit 9c9fb34 into nitrojs:main May 10, 2022
@TotomInc TotomInc deleted the feature/add-prerender-route-hook branch May 10, 2022 14:37
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

Successfully merging this pull request may close these issues.

2 participants