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: generate .assetsignore file for Cloudflare deployment #13109

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

Conversation

methanoya
Copy link

@methanoya methanoya commented Dec 5, 2024

It is corresponded to cloudflare/workers-sdk#6640

Resolves #13108

This PR avoids Cloudflare deployment errors like Uploading a Pages _worker.js file as an asset by adding .assetsignore file generation.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it. These adapters don't appear to have tests.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@benmccann benmccann changed the title .assetsignore file generation for Cloudflare deployment. It is corres… feat: generate .assetsignore file for Cloudflare deployment Dec 6, 2024
@benmccann
Copy link
Member

And feature added to the cloudflare adapter must be added to the cloudflare workers adapter as well

Copy link

changeset-bot bot commented Dec 7, 2024

🦋 Changeset detected

Latest commit: 3b27f87

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

This PR includes changesets to release 1 package
Name Type
@sveltejs/adapter-cloudflare Minor

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

@methanoya
Copy link
Author

And feature added to the cloudflare adapter must be added to the cloudflare workers adapter as well

I was not completely sure that the adapter-cloudflare-workers is affected but it is updated for now.

@methanoya methanoya requested a review from benmccann December 7, 2024 01:07
@eltigerchino
Copy link
Member

eltigerchino commented Dec 10, 2024

I misread the original issue. Does this only affect the workers adapter since it's a workers only feature? https://developers.cloudflare.com/workers/static-assets/binding/#ignoring-assets

@@ -143,6 +143,8 @@ export default function ({ config = 'wrangler.toml', platformProxy = {} } = {})
);
}

copyFileSync(`${files}/.assetsignore`, `${dirname(main)}/.assetsignore`);
Copy link
Member

@eltigerchino eltigerchino Dec 10, 2024

Choose a reason for hiding this comment

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

Seems like we would need #13072 to be merged first, then copy the .assetsignore file to the root of the assets folder.

https://developers.cloudflare.com/workers/static-assets/binding/#ignoring-assets

In this case, create a .assetsignore file in the root of the assets directory.

I'm also wondering if we would want to simply append / write to a file instead of copying so that a user's .assetsignore file would be taken into consideration. Similar to

writeFileSync(`${dest}/_headers`, generate_headers(builder.getAppPath()), { flag: 'a' });
where we append the generated _headers to the file.

Copy link
Author

Choose a reason for hiding this comment

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

It looks like #13072 is in a draft state, this PR is ready to use.
The source of the .assetsignore is https://github.com/cloudflare/workers-sdk/blob/main/packages/create-cloudflare/templates-experimental/svelte/js/static/.assetsignore They created it some way, so it makes sense to keep it the same and copy from there if updated. It is a better point to control the list of server-side-only files. Another approach is adding files like _worker.js to the .assetsignore once they are created, but it is probably less clear. Let me know if you have a strong opinion, I will update the PR accordingly.

Copy link
Member

@eltigerchino eltigerchino Dec 11, 2024

Choose a reason for hiding this comment

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

The difference is that Cloudflare's .assetsignore file is part of a starting template, whereas we need to take into account a user having their own file. I'd recommend that we write using append mode so it's created if it doesn't exist, otherwise it writes to the existing file.

I'm also not sure copying it to main corresponds to the root of the assets directory. Isn't it currently site.bucket?

builder.log.minor('Copying assets...');
const bucket_dir = `${site.bucket}${builder.config.kit.paths.base}`;
builder.writeClient(bucket_dir);
builder.writePrerendered(bucket_dir);

But I'm also not sure if copying it to the bucket root would have any effect since it's part of the old Workers sites feature. cc: @petebacondarwin

Copy link
Author

Choose a reason for hiding this comment

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

What about a shipping update for adapter-cloudflare only since it would work differently for adapter-cloudflare-workers and can depend on #13072 ?

Copy link
Author

@methanoya methanoya Dec 11, 2024

Choose a reason for hiding this comment

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

Making .assetsignore based on an existing one can be a good idea, but it looks like adapter-cloudflare takes responsibility for making the right bundle for the Cloudflare deployment. I am not sure that a user can have a reason to make any non-asset file.
We can show a warning message if .assetsignore is already present as a current workaround.
I am open to both approaches if maintainers strongly believe either way.

Copy link
Member

@eltigerchino eltigerchino Dec 12, 2024

Choose a reason for hiding this comment

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

What about a shipping update for adapter-cloudflare only since it would work differently for adapter-cloudflare-workers and can depend on #13072 ?

Yeah, I think we can probably split the Workers part into another PR.

Making .assetsignore based on an existing one can be a good idea, but it looks like adapter-cloudflare takes responsibility for making the right bundle for the Cloudflare deployment. I am not sure that a user can have a reason to make any non-asset file. We can show a warning message if .assetsignore is already present as a current workaround. I am open to both approaches if maintainers strongly believe either way.

The main purpose of the file is to prevent certain assets from being made public. This can be desirable if the user has server only assets. I think we should append to an existing .assetsignore file

Copy link
Author

Choose a reason for hiding this comment

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

@eltigerchino I have removed the -workers code from the PR and added .assetsignore merging. It may be overcomplicated, and we would need to add just one file to another. Let me know your thoughts.

@methanoya
Copy link
Author

methanoya commented Dec 10, 2024

I misread the original issue. Does this only affect the workers adapter since it's a workers only feature? https://developers.cloudflare.com/workers/static-assets/binding/#ignoring-assets

It definitely affects adapter-cloudflare since wrangler blocks the deployment with the adapter without the fix or workaround. See #13108. I am not completely sure about -worker. I would suggest to put pkg:adapter-cloudflare label back for both PR and the issue reported

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.

.assetsignore support on adding adapter-cloudflare to
3 participants