Skip to content

[WIP] Add support for skew protection #746

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

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

Conversation

vicb
Copy link
Contributor

@vicb vicb commented Jun 20, 2025

There are 2 main files in this PR:

packages/cloudflare/src/cli/commands/skew-protection.ts (build time)

This builds a mapping from deploymentId to the worker version at build time.
Note that because the worker you are building has no version yet, "current" is used instead.

packages/cloudflare/src/cli/templates/skew-protection.ts (runtime)

At runtime, if a particular version is requested and it is present in the mapping,
we'll fetch the result from a preview URL with a hostname of <version>-<worker_name>.<domain>.workers.dev

How to use it:

  • set cloudflare.skewProtectionEnabled to true in your OpenNext config
  • provide values for the new env vars added to packages/cloudflare/src/api/cloudflare-context.ts
  • update your next config to set the deployementId - you can use the getDeploymentId() helper
  • set run_worker_first to true

Docs PR opennextjs/docs#164

TODO:

  • add tests
  • handle assets (will need to run the worker before the asset worker)
  • follow-up: move the skew protection logic in the aws/routing layer with pluggable overrides

Copy link

changeset-bot bot commented Jun 20, 2025

⚠️ No Changeset found

Latest commit: 468cd0a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

Copy link

pkg-pr-new bot commented Jun 20, 2025

Open in StackBlitz

npm i https://pkg.pr.new/@opennextjs/cloudflare@746

commit: 43be1dd

@vicb vicb force-pushed the vicb/skew-protection branch from 2356748 to 0a6f2dd Compare June 20, 2025 13:11
@vicb vicb requested review from conico974, james-elicx, dario-piotrowicz and sommeeeer and removed request for conico974 June 20, 2025 13:11
@vicb vicb force-pushed the vicb/skew-protection branch from 0a6f2dd to 18ba05a Compare June 20, 2025 13:14
@vicb vicb requested a review from IgorMinar June 20, 2025 13:26
Copy link
Collaborator

@conico974 conico974 left a comment

Choose a reason for hiding this comment

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

I just took a really quick look for now, will make a proper review later

Copy link
Collaborator

@james-elicx james-elicx left a comment

Choose a reason for hiding this comment

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

I think this is looking pretty good on my initial read though. Going to have another look later on as well.

Copy link
Collaborator

@james-elicx james-elicx left a comment

Choose a reason for hiding this comment

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

I think this looks good and the logic makes sense to me.

It might be nice for us to look at having an e2e that runs against a deployment at some point in the future, for testing features like this.

@vicb vicb force-pushed the vicb/skew-protection branch 4 times, most recently from 4c21542 to 471111c Compare June 23, 2025 12:13
@vicb vicb changed the title [Preview] Add support for skew protection Add support for skew protection Jun 24, 2025
Copy link
Collaborator

@sommeeeer sommeeeer left a comment

Choose a reason for hiding this comment

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

LGTM

@vicb
Copy link
Contributor Author

vicb commented Jun 24, 2025

Thanks for the review @sommeeeer !

Copy link
Collaborator

@conico974 conico974 left a comment

Choose a reason for hiding this comment

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

Just a few nits/question, other than that LGTM

* @param paths The list of path
* @returns The root node of the tree
*/
export function filesToTree(paths: string[]): FolderNode {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think in the future here, we should create glob pattern whenever we can to avoid having a too big FolderNode . This could grow quite big if people have a lot of assets

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'll create a follow up issue for that.

* @param options Options to pass to `getPlatformProxy`, i.e. to set the environment
* @returns the env vars
*/
export async function getEnvFromPlatformProxy(options: GetPlatformProxyOptions) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should have only one instance of this, that we spawn at the beginning and that we dispose at the end. It's a bit wasteful to launch multiple workerd process.
And once we have support for remote bindings here, we could reuse it to populate the cache directly (which should be way faster)

client,
accountId,
afterTimeMs = new Date().getTime() - MAX_VERSION_AGE_DAYS * 24 * 3600 * 1000,
maxNumberOfVersions = MAX_NUMBER_OF_VERSIONS,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this one should be overridable

path = path.slice(basePath.length);
}
if (path.startsWith("/_next/static/") || isFileInTree(path, __CF_ASSETS_TREE__)) {
return assets.fetch(request);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this one respect the _headers file ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should but I'll test that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vicb vicb force-pushed the vicb/skew-protection branch from 2f1f47b to 45edc9f Compare June 30, 2025 12:50
@vicb vicb changed the title Add support for skew protection [WIP] Add support for skew protection Jul 4, 2025
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.

4 participants