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(netlify): support included/excluded files #325

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

Conversation

ascorbic
Copy link
Contributor

Changes

Adds "includedFiles" and "excludedFiles" options. These allow extra files to be deployed in the SSR function bundle. It globs these files at build time, and includes them in the file list that is deployed in the function. This PR also ensures that process.cwd matches the site root during SSR.

Testing

Adds new test fixture and tests.

Docs

This needs docs. The changeset includes a lot of the required info, as do the types.

Copy link

changeset-bot bot commented Jul 11, 2024

🦋 Changeset detected

Latest commit: 1a2319f

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 Major
@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

.changeset/itchy-donuts-love.md Outdated Show resolved Hide resolved
packages/netlify/src/index.ts Show resolved Hide resolved
return async function handler(request: Request, context: Context) {
// This entrypoint will be deep inside the directory structure below cwd
// We chdir to the root so that we can resolve the correct paths at runtime
process.chdir(root);
Copy link
Member

Choose a reason for hiding this comment

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

Are we sure it can't break any existing app?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I can work out, existing apps that rely on cwd would either have unchanged behaviour (if they're not a monorepo), or would already be broken (if they are using a monorepo)

Copy link
Member

Choose a reason for hiding this comment

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

I don't understand what the "correct paths at runtime" refers to here. It feels dangerous to call chdir() as a request handler. Can you explain where is resolving from the cwd?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If a site uses filesystem functions at runtime, this is the only way it can correctly load the files

Copy link
Member

@bluwy bluwy Jul 24, 2024

Choose a reason for hiding this comment

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

I don't think using filesystem functions is something Astro supports in the runtime? Files could be moved during bundling and it wouldn't work later.

Is process.chdir() needed for this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't prevent people using fs functions, so I'd imagine lots of people do. This PR was prompted by an issue where a library was trying to load i18n data and it wasn't found. This part fixes precisely that issue: of files being moved during bundling. Without that there's not a lot of point to allowing users to include files in the bundle, because the user code won't be able to find them.

Copy link
Member

@bluwy bluwy Jul 24, 2024

Choose a reason for hiding this comment

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

I don't feel like fs functions are a common thing especially when dealing in a bundled runtime. There's Vite APIs like import.meta.glob, ?url, and ?raw for them. Fs functions are also likely to fail elsewhere than only on Netlify, and so we shouldn't support and encourage them.

I figured the benefit with includeFiles and excludeFiles are like Vercel's option, because nft has trouble sometimes detecting every file. Especially inside node_modules where libraries use fs relatively, those code are not processed by Vite so we could use the option for them, however if they resolve relatively I'm not sure how cwd helps here. And if the library do resolve from cwd, it would've been generally a problem if an Astro is also hosted on a subdirectory or any other fine setup.

I'm not really comfortable with keeping this in the adapter. It feels surprising to me that it could change the cwd internally and on every request.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are like Vercel's option. In fact they use almost the same implementation. With libraries that use fs functions, they usually accept a directory as an option or resolve relative to cwd. It's very common for i18n libraries such as i18next, for example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we're about to do a major I can do a breaking changeset for the chdir part.

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 looks super helpful, @ascorbic and the docs are fabulous!

Just a couple of quick thoughts mostly on formatting.

(In particular, we do tend to use inline code when naming a feature, not quotation marks. And both inline AND quotation marks are reserved for when that's how the feature would be shown in the reader's code (e.g. output: 'server'. (Though, we're more relaxed with hybrid and server just written on their own.)

Otherwise, great intro, great explanation, and helpful code example. What more could I possibly want? 😄

.changeset/itchy-donuts-love.md Outdated Show resolved Hide resolved
.changeset/itchy-donuts-love.md Outdated Show resolved Hide resolved
@ascorbic
Copy link
Contributor Author

!preview include-files

Copy link
Contributor

Snapshots have been released for the following packages:

  • @astrojs/netlify@experimental--include-files
Publish Log
🦋  warn ===============================IMPORTANT!===============================
🦋  warn Packages will be released under the experimental--include-files tag
🦋  warn ----------------------------------------------------------------------
🦋  info npm info @astrojs/cloudflare
🦋  info npm info @astrojs/netlify
🦋  warn @astrojs/cloudflare is not being published because version 11.0.1 is already published on npm
🦋  info @astrojs/netlify is being published because our local version (0.0.0-include-files-20240712111209) has not been published on npm
🦋  info Publishing "@astrojs/netlify" at "0.0.0-include-files-20240712111209"
🦋  success packages published successfully:
🦋  @astrojs/[email protected]
🦋  Creating git tag...
🦋  New tag:  @astrojs/[email protected]
Build Log
 WARN  Unsupported engine: wanted: {"node":">=20.15.0"} (current: {"node":"v18.20.3","pnpm":"9.4.0"})

> [email protected] build /home/runner/work/adapters/adapters
> turbo run build --filter="@astrojs/*"

• Packages in scope: @astrojs/cloudflare, @astrojs/netlify, @astrojs/test-utils
• Running build in 3 packages
• Remote caching disabled
::group::@astrojs/netlify:build
cache miss, executing 263e5d99e0f2b801

> @astrojs/[email protected] build /home/runner/work/adapters/adapters/packages/netlify
> tsc

::endgroup::
::group::@astrojs/cloudflare:build
cache miss, executing 81371b8c47fd6c9f

> @astrojs/[email protected] build /home/runner/work/adapters/adapters/packages/cloudflare
> tsc

::endgroup::

 Tasks:    2 successful, 2 total
Cached:    0 cached, 2 total
  Time:    3.605s 

@alexanderniebuhr
Copy link
Member

@ascorbic Do you want to land this before the next minor of the Netlify Adapter?

@alexanderniebuhr alexanderniebuhr mentioned this pull request Jul 23, 2024
@ascorbic
Copy link
Contributor Author

@alexanderniebuhr Ideally, yes. It's still waiting on an approval

@alexanderniebuhr
Copy link
Member

I already looked at it and quite liked it, but I can review it later today, so we can land it before we cut the release :)

@alexanderniebuhr alexanderniebuhr self-requested a review July 23, 2024 09:48
@ascorbic ascorbic requested a review from ematipico July 24, 2024 09:15
Copy link
Member

@alexanderniebuhr alexanderniebuhr left a comment

Choose a reason for hiding this comment

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

I miss specific platform knowledge but I like the feature and implementation looks good and has tests.

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 left a suggestion of a more compact style, if you'd like one!

.changeset/itchy-donuts-love.md Outdated Show resolved Hide resolved
.changeset/itchy-donuts-love.md Outdated Show resolved Hide resolved
.changeset/itchy-donuts-love.md Outdated Show resolved Hide resolved
Co-authored-by: Sarah Rainsberger <[email protected]>
@alexanderniebuhr
Copy link
Member

I was not able to wait for this PR to make it into the release, I needed to ship Cloudflare bugfixes.. Are all discussions resolved?

@sarah11918
Copy link
Member

sarah11918 commented Aug 5, 2024

The changeset is fine by docs, but we will need documentation in the Netlify adapter page in docs. A new <h3> under Usage seems like a good place, I think? https://docs.astro.build/en/guides/integrations-guide/netlify/#usage

As Matt wrote, it can take very much the form of what's in the changeset, plus any other important characteristics to document (because I said the changeset doesn't have to be exhaustive -- but there may be other details appropriate for full documentation).

@ascorbic
Copy link
Contributor Author

ascorbic commented Sep 5, 2024

!preview include-files

@withastro withastro deleted a comment from github-actions bot Sep 5, 2024
Copy link
Contributor

github-actions bot commented Sep 5, 2024

Snapshots have been released for the following packages:

  • @astrojs/netlify@experimental--include-files
Publish Log
🦋  warn ===============================IMPORTANT!===============================
🦋  warn Packages will be released under the experimental--include-files tag
🦋  warn ----------------------------------------------------------------------
🦋  info npm info @astrojs/cloudflare
🦋  info npm info @astrojs/netlify
🦋  info npm info @astrojs/node
🦋  info npm info @astrojs/vercel
🦋  warn @astrojs/cloudflare is not being published because version 11.0.4 is already published on npm
🦋  info @astrojs/netlify is being published because our local version (0.0.0-include-files-20240905130315) has not been published on npm
🦋  warn @astrojs/node is not being published because version 8.3.3 is already published on npm
🦋  warn @astrojs/vercel is not being published because version 7.8.0 is already published on npm
🦋  info Publishing "@astrojs/netlify" at "0.0.0-include-files-20240905130315"
🦋  success packages published successfully:
🦋  @astrojs/[email protected]
🦋  Creating git tag...
🦋  New tag:  @astrojs/[email protected]
Build Log

> [email protected] build /home/runner/work/adapters/adapters
> turbo run build --filter="@astrojs/*"

• Packages in scope: @astrojs/cloudflare, @astrojs/netlify, @astrojs/node, @astrojs/test-utils, @astrojs/vercel
• Running build in 5 packages
• Remote caching disabled
::group::@astrojs/node:build
cache miss, executing 500254f597799cf1

> @astrojs/[email protected] build /home/runner/work/adapters/adapters/packages/node
> tsc

::endgroup::
::group::@astrojs/netlify:build
cache miss, executing 2759776ada86b638

> @astrojs/[email protected] build /home/runner/work/adapters/adapters/packages/netlify
> tsc

::endgroup::
::group::@astrojs/vercel:build
cache miss, executing 6e63e7a22cb615dd

> @astrojs/[email protected] build /home/runner/work/adapters/adapters/packages/vercel
> tsc

::endgroup::
::group::@astrojs/cloudflare:build
cache miss, executing b168b1ba31cb4844

> @astrojs/[email protected] build /home/runner/work/adapters/adapters/packages/cloudflare
> tsc

::endgroup::

 Tasks:    4 successful, 4 total
Cached:    0 cached, 4 total
  Time:    6.222s 

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

Successfully merging this pull request may close these issues.

6 participants