Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Update netlify adapter to integrate includeFiles and excludeFiles options #515

Closed
wants to merge 6 commits into from

Conversation

dfdez
Copy link

@dfdez dfdez commented Jan 22, 2025

Changes

When trying Netlify SSR with Astro, I wasn't able to get the included_files option to work. After investigating, I realized that the includeFiles and excludeFiles functionalities, similar to what Vercel provides, were necessary to include additional files during the build process.

In this PR, I have implemented the integration to add these options:

  • Added includeFiles and excludeFiles options to the integration, mirroring Vercel's functionality.
    • includeFiles: Allows users to explicitly specify files or directories to be bundled with their function. Useful for avoiding missing files in production.
    • excludeFiles: Enables users to specify files or directories to be excluded from the deployment. Helps minimize bundle size.
  • All code for these features has been adapted directly from Vercel's integration for consistency and reliability.

Testing

  • Manually tested includeFiles and excludeFiles with various configurations to ensure:
    • Files specified in includeFiles are bundled correctly.
    • Files and directories specified in excludeFiles are omitted as expected.
  • Automated tests added to validate edge cases and typical usage scenarios.
  • Confirmed the implementation behaves identically to Vercel's integration.

Docs

  • Updated astro docs with documentation for includeFiles and excludeFiles on netlify adapter.
  • Added links to Vercel's documentation for further reference:

Copy link

changeset-bot bot commented Jan 22, 2025

🦋 Changeset detected

Latest commit: 5557cc0

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

This PR includes changesets to release 2 packages
Name Type
@astrojs/netlify Minor
@test/netlify-hosted-astro-project Patch

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

@dfdez dfdez changed the title feat: integrated includeFiles on netlify adapter feat: integrated includeFiles and excludeFiles on netlify adapter Jan 22, 2025
@dfdez dfdez changed the title feat: integrated includeFiles and excludeFiles on netlify adapter Update netlify adapter to integrate includeFiles and excludeFiles options Jan 22, 2025
@dfdez dfdez marked this pull request as ready for review January 22, 2025 21:29
@ematipico
Copy link
Member

Automated tests added to validate edge cases and typical usage scenarios.

It doesn't seem you added new assertions, and you didn't test the excludeFiles. Can you please do so?

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.

Can you please add a new changeset?

@ascorbic
Copy link
Contributor

I have an old PR that does the same, but it also included changing the cwd at runtime, which in retropect should have been separate – and proved to be controversial – which is why I never merged it. Mine has now become outdated and conflicted, so having a new PR like this makes sense to me, but you might want to look at my PR to get some tests and changeset ideas.

@dfdez
Copy link
Author

dfdez commented Jan 24, 2025

Thanks @ascorbic for the PR was very useful ✨

After taking a look to #325 I have made the following changes:

@dfdez dfdez requested a review from ematipico January 24, 2025 01:24

The `includedFiles` and `excludedFiles` options allow you specify these inclusions and exclusions as an array of file paths relative to the site root. Both options support glob patterns, so you can include/exclude multiple files at once.

If you are specifying files using filesystem functions, resolve the path using `path.resolve()` or `process.cwd()`, which will give you the site root. At runtime, compiled source files will be in a different location and you cannot rely on relative file paths.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this will work in this PR, because it doesn't do chdir

Copy link
Member

Choose a reason for hiding this comment

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

I can't speak to the tech, but will note:

  • this feels a bit too detailed for a changelog
  • if someone needs an explanation like this to use the feature, it must be in docs itself

Copy link
Author

Choose a reason for hiding this comment

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

Changelogs update -> 5557cc0


export default defineConfig({
vite: {
assetsInclude: process.env.VITE_ASSETS_INCLUDE?.split(',') || [],
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than juggling env vars you can pass the actual config to loadFixture in each test

Copy link
Author

Choose a reason for hiding this comment

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

Done 👍🏽 -> 63ce3bb

Comment on lines 20 to 21
process.env.VITE_ASSETS_INCLUDE = expectedAssetsInclude.join();
fixture = await loadFixture({ root });
Copy link
Contributor

Choose a reason for hiding this comment

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

Pass the config to loadFixture instead

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.

Just some tiny comments on the changeset, and info that seems to be in this PR that did not make it into docs!


Adds `includedFiles` and `excludedFiles` configuration options. These allow extra files to be deployed in the SSR function bundle.

When an Astro site using `server` or `hybrid` rendering is deployed to Netlify, the generated functions trace the server dependencies and include any that may be needed in SSR. However, sometimes you may want to include extra files that are not detected as dependencies, such as files that are loaded using `fs` functions. Also, you may sometimes want to specifically exclude dependencies that are bundled automatically. For example, you may have a Node module that includes a large binary.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
When an Astro site using `server` or `hybrid` rendering is deployed to Netlify, the generated functions trace the server dependencies and include any that may be needed in SSR. However, sometimes you may want to include extra files that are not detected as dependencies, such as files that are loaded using `fs` functions. Also, you may sometimes want to specifically exclude dependencies that are bundled automatically. For example, you may have a Node module that includes a large binary.
When an Astro site using on-demand rendering is deployed to Netlify, the generated functions trace the server dependencies and include any that may be needed in SSR. However, sometimes you may want to include extra files that are not detected as dependencies, such as files that are loaded using `fs` functions. Also, you may sometimes want to specifically exclude dependencies that are bundled automatically. For example, you may have a Node module that includes a large binary.

Noting that hybrid does not exist as a "code" word anymore (and, I'm not sure we use it casually?)


When an Astro site using `server` or `hybrid` rendering is deployed to Netlify, the generated functions trace the server dependencies and include any that may be needed in SSR. However, sometimes you may want to include extra files that are not detected as dependencies, such as files that are loaded using `fs` functions. Also, you may sometimes want to specifically exclude dependencies that are bundled automatically. For example, you may have a Node module that includes a large binary.

The `includedFiles` and `excludedFiles` options allow you specify these inclusions and exclusions as an array of file paths relative to the site root. Both options support glob patterns, so you can include/exclude multiple files at once.
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 a note (in the Docs PR) that this content did not make it into the docs PR, but should!


The `includedFiles` and `excludedFiles` options allow you specify these inclusions and exclusions as an array of file paths relative to the site root. Both options support glob patterns, so you can include/exclude multiple files at once.

If you are specifying files using filesystem functions, resolve the path using `path.resolve()` or `process.cwd()`, which will give you the site root. At runtime, compiled source files will be in a different location and you cannot rely on relative file paths.
Copy link
Member

Choose a reason for hiding this comment

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

I can't speak to the tech, but will note:

  • this feels a bit too detailed for a changelog
  • if someone needs an explanation like this to use the feature, it must be in docs itself

@dfdez
Copy link
Author

dfdez commented Jan 30, 2025

@ascorbic I've made the requested changes to the tests, and as @sarah11918 requested, I've updated the changelog and made some updates to the docs as well. 👍🏽

Let me know if you need anything else! Thanks!

@dfdez dfdez requested review from sarah11918 and ascorbic January 30, 2025 22:45
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.

I am not really confident about this feature because I don't know the code. I let @ascorbic doing th review

@ematipico ematipico dismissed their stale review January 31, 2025 15:36

Not required anymore

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.

Changeset is good! It could merge exactly like this, but I could even see adding a code example that shows both new properties configured (I'd maybe include one file, to show that it's still an array even if you only have one item, and then exclude two or three files, and/or a glob pattern?), but I'd usually make final recommendations when the feature is ready to be merged and the docs are done.

I can see that this feature still needs a code review/approval, so I won't super edit the corresponding docs just yet, but can confirm they're in good draft shape and I should have all that I need to whip those up if this feature is being accepted! (I do like to wait until it's pretty clear nothing more will change before doing a final polish to the documentation, just in case!)

So please ping me when this is close to being released, and I'll come in for a final swoop on both this and docs then! It won't take much! 🫡

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.

Looks good! Once this is in, you might want to think about your proposal for exposing the fucntion root dir at runtime. If we were to do it, it would make sense to do the same for Vercel.

@ematipico
Copy link
Member

Hi, we moved the netlify adapter in a new repository: https://github.com/withastro/astro/tree/main/packages/integrations/netlify

We will achieve this repository soon, so if you wish to land this PR, you'll have to create a new one.

@ematipico ematipico closed this Feb 7, 2025
@ascorbic
Copy link
Contributor

ascorbic commented Feb 7, 2025

@dfdez sorry we didn't get this in before the move. It should be fine to redo it as-is on the main repo, aside from the slightly changed base path

@dfdez
Copy link
Author

dfdez commented Feb 7, 2025

No worries! I'll redo it on the main repo 👍🏽

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants