Skip to content

feat: add Vite plugin to Netlify adapter#13768

Merged
ascorbic merged 24 commits intowithastro:mainfrom
eduardoboucas:eb/netlify-vite-plugin
Jul 10, 2025
Merged

feat: add Vite plugin to Netlify adapter#13768
ascorbic merged 24 commits intowithastro:mainfrom
eduardoboucas:eb/netlify-vite-plugin

Conversation

@eduardoboucas
Copy link
Contributor

Changes

Loads the new (and experimental) Netlify Vite plugin, which means a few things:

  1. The environment is populated with the Netlify global, as well as the globals needed to use Netlify Blobs and the upcoming Netlify Cache API straight from astro dev, without requiring the Netlify CLI
  2. Starting up astro dev will make the plugin automatically add the .netlify directory to the .gitignore file, like the Netlify CLI does
  3. The plugin will suggest that the user links the project is linked to a Netlify site, if it isn't already linked

I'm keeping it as draft for now because I still need to add some tests and update docs (see below). And above all, I'd love to hear your feedback, since this is a net new flow we're introducing.

Testing

I'd like to add some tests, but from a quick look it seems that the existing tests only assert the build output. Do we have a way to test the development flow?

Docs

I think we should update docs for this, both on the Astro side and on the Netlify side, explaining what will now become available during the local development process.

/cc @withastro/maintainers-docs for feedback!

@changeset-bot
Copy link

changeset-bot bot commented May 8, 2025

🦋 Changeset detected

Latest commit: 147b2ab

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: integration Related to any renderer integration (scope) label May 8, 2025
@ascorbic
Copy link
Contributor

ascorbic commented May 8, 2025

Cool! What happens if a user runs it via the Netlify CLI? Will it end up running both? Maybe it should be conditionally loaded in that case

@eduardoboucas
Copy link
Contributor Author

Cool! What happens if a user runs it via the Netlify CLI? Will it end up running both? Maybe it should be conditionally loaded in that case

Good point! We should be able to use environment variables to detect whether we're running inside the Netlify CLI and make it a no-op in that case. I just need to think of the best place to check that.

@eduardoboucas
Copy link
Contributor Author

Updated in 0630323 via netlify/primitives#181.

@ascorbic
Copy link
Contributor

ascorbic commented May 9, 2025

Take a look at this for an example of running dev mode tests:

https://github.com/withastro/astro/blob/main/packages/astro/test/sessions.test.js#L135

@ascorbic
Copy link
Contributor

ascorbic commented Jun 4, 2025

@eduardoboucas now the plugin is no longer experimental, do you want to finish off the PR and get this merged?

@eduardoboucas eduardoboucas marked this pull request as ready for review June 17, 2025 16:49
@eduardoboucas
Copy link
Contributor Author

@eduardoboucas now the plugin is no longer experimental, do you want to finish off the PR and get this merged?

Yes! I've just updated the version of the plugin and added some tests.

I'm sure there's lots more we can do to make the integration even nicer, but I think this would be a good first step. Let me know your thoughts!

Copy link
Contributor

@serhalp serhalp left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

Comment on lines +6 to +9
adapter: netlify({
edgeMiddleware: process.env.EDGE_MIDDLEWARE === 'true',
imageCDN: process.env.DISABLE_IMAGE_CDN ? false : undefined,
}),
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: AFAICT from spelunking these are unused here

Suggested change
adapter: netlify({
edgeMiddleware: process.env.EDGE_MIDDLEWARE === 'true',
imageCDN: process.env.DISABLE_IMAGE_CDN ? false : undefined,
}),
adapter: netlify(),

@@ -0,0 +1,2 @@
# Local Netlify folder
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is in the wrong place

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed in 087d488.

Copy link
Contributor

@ascorbic ascorbic left a comment

Choose a reason for hiding this comment

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

Thanks! There are a few places in the adapter code where we check if we're running in the Netlify CLI before enabling/disabling various options. e.g. when it's not running in the Netlify CLI, we don't use the image CDN or blobs. These checks should be removed so we can use the primitives in dev.

@ascorbic
Copy link
Contributor

ascorbic commented Jun 18, 2025

The tests are timing-out because they're never exiting. I'm not sure why, because it is stopping the dev server. Did this happen for you when you ran them locally?

Copy link
Contributor

@ascorbic ascorbic left a comment

Choose a reason for hiding this comment

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

It looks like something isn't being cleaned-up, meaning Node never exits. Presumably one of the Netlify servers? Is there a way we can ensure that they all shut down when the parent dev server exits?

@ematipico
Copy link
Member

The tests are timing-out because they're never exiting. I'm not sure why, because it is stopping the dev server. Did this happen for you when you ran them locally?

It's possible that this branch was created from an outdated main branch.

@eduardoboucas
Copy link
Contributor Author

eduardoboucas commented Jun 18, 2025

The hanging tests were caused by an issue on our side, which I have fixed in netlify/primitives#310.

I'm expecting them to pass now. ⏳

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Thank you @eduardoboucas, for the PR. Fortuantely the code changes are minimal, which they seem good. However, I have some concerns regarding docs and expectations. Could you clarify them?

},
session,
vite: {
plugins: [netlifyVitePlugin()],
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we add this plugin only when running the dev command? That's what I understood from the PR description

Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like the plugin only implements the configureServer hook, so it won't affect builds: https://github.com/netlify/primitives/blob/main/packages/vite-plugin/src/main.ts

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm happy to make this more explicit on the adapter side if needed. What's your recommendation?

Copy link
Member

Choose a reason for hiding this comment

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

I think just a mention in the docs of the plugin is more than enough :)

Copy link
Member

Choose a reason for hiding this comment

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

I suppose this file and netlify/functions/func.mjs are part of the Netlify Vite plugin; however, the plugin's documentation doesn't mention these files.

I would appreciate if you could update the PR description, and guide us reviewers to understand what these files are for, and what we should expect.

Additionally, is there a link we can use to redirect our users to instructions on how to use these files?

Copy link
Contributor

Choose a reason for hiding this comment

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

Those are just regular Netlify functions and edge functions, rather than anything related to the Vite plugin specifically. There's some info here: https://docs.astro.build/en/guides/deploy/netlify/#using-netlify-functions

Copy link
Member

Choose a reason for hiding this comment

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

What should we expect from these functions though?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think just that they work as expected on a Netlify site (i.e. they run when they match a route)

@eduardoboucas
Copy link
Contributor Author

Thanks! There are a few places in the adapter code where we check if we're running in the Netlify CLI before enabling/disabling various options. e.g. when it's not running in the Netlify CLI, we don't use the image CDN or blobs. These checks should be removed so we can use the primitives in dev.

@ascorbic Mind if we do this as a follow-up? I'd love to get the skeleton landed first.

@chee chee force-pushed the eb/netlify-vite-plugin branch from e192940 to 2825dc3 Compare June 30, 2025 17:41
@chee chee force-pushed the eb/netlify-vite-plugin branch from 2825dc3 to dd479d7 Compare June 30, 2025 17:43
@chee
Copy link
Contributor

chee commented Jun 30, 2025

yay

@ascorbic
Copy link
Contributor

ascorbic commented Jul 1, 2025

It looks like this is ready! Can you do a changeset? We should be able to get this in 5.11, and it would be great to be able to highlight this in the blog. The changeset should be written for the audience of end users, so rather than explaining the technical implementation in any detail (beyond saying that it now uses the Netlify Vite plugin), focus on what this means for the user.

Are there any changes needed for docs? Does it change any of the instructions in the deployment guide or adapter docs?

image: {
service: {
entrypoint: enableImageCDN ? '@astrojs/netlify/image-service.js' : undefined,
entrypoint: integrationConfig?.imageCDN ? '@astrojs/netlify/image-service.js' : undefined,
Copy link
Contributor

Choose a reason for hiding this comment

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

This should default to true, so something like this (with a comment explaining why):

Suggested change
entrypoint: integrationConfig?.imageCDN ? '@astrojs/netlify/image-service.js' : undefined,
entrypoint: integrationConfig?.imageCDN === false ? undefined : '@astrojs/netlify/image-service.js',

@chee chee force-pushed the eb/netlify-vite-plugin branch from d584b7c to 364ac21 Compare July 7, 2025 16:50
@chee chee force-pushed the eb/netlify-vite-plugin branch from 85679b9 to 2f0cfd8 Compare July 9, 2025 07:15
@chee chee force-pushed the eb/netlify-vite-plugin branch from 2f0cfd8 to b835391 Compare July 10, 2025 09:04
Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

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

This sounds fantastic! 🙌

Left some thoughts for the changeset for your consideration!

Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
@ascorbic ascorbic merged commit faa0eff into withastro:main Jul 10, 2025
15 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Jul 10, 2025
@chee chee deleted the eb/netlify-vite-plugin branch July 10, 2025 18:45
@ascorbic ascorbic added this to the v5.12.0 milestone Jul 14, 2025
openscript pushed a commit to openscript/astro that referenced this pull request Sep 12, 2025
* feat: load Netlify Vite plugin

* chore: update `@netlify/vite-plugin`

* update plugin and add tests

* Update pnpm-lock.yaml

* remove unused code

* Deps

* update plugin

* remove unused file

* Apply suggestions from code review

Co-authored-by: Matt Kane <m@mk.gg>

* Upgrade vite plugin

* satisfy linter

* Upgrade vite plugin

* refactor: use Netlify primitives

* make imageCDN an opt out, not an opt in

* remove outside netlify test, for now we are never outside

* update pnpm-lock

* invalidate the astro session when restarting dev server

* Update to latest vite plugin, functions and blobs

* Add changeset for @astrojs/netlify on @netlify/vite-plugin

* apply Sarah's changeset improvements

Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>

---------

Co-authored-by: Matt Kane <m@mk.gg>
Co-authored-by: chee <chee.rabbits@netlify.com>
Co-authored-by: chee <chee@rabbits.computer>
Co-authored-by: chee <yay@chee.party>
Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: integration Related to any renderer integration (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants