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

fix: middleware being invoked more than once in a phase #337

Merged

Conversation

james-elicx
Copy link
Contributor

This PR does the following:

  • Introduces an array to track which middleware has been invoked during each run of a phase.
  • If a middleware is tried to be invoked more than once, it skips.
  • Adds a test.

In the build output config, there are sometimes two records created for middleware in the none phase, both of which can be hit and then accidentally lead to multiple invocations. This PR introduces an array that tracks which middleware paths have been invoked when running a phase. If it tries to use one that has already been invoked, it will skip doing so, fixing the issue of multiple invocations.

@changeset-bot
Copy link

changeset-bot bot commented Jun 26, 2023

🦋 Changeset detected

Latest commit: 554a0db

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

This PR includes changesets to release 2 packages
Name Type
@cloudflare/next-on-pages Patch
eslint-plugin-next-on-pages 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

@github-actions
Copy link
Contributor

🧪 Prereleases are available for testing 🧪

@cloudflare/next-on-pages

You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/next-on-pages/runs/5377487171/npm-package-next-on-pages-337

@cloudflare/eslint-plugin-next-on-pages

You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/next-on-pages/runs/5377487171/npm-package-eslint-plugin-next-on-pages-337

Comment on lines +43 to +44
/** Tracker for the middleware that have been invoked in a phase */
private middlewareInvoked: string[];
Copy link
Member

Choose a reason for hiding this comment

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

should/cloud this be a flag instead?

as in

private middlewareInvoked: boolean;

and you set it to true/false as needed?
do we need to check/care that the middlewarePath is the same? shouldn't we just prevent multiple middlewares from running regardless?

Copy link
Member

Choose a reason for hiding this comment

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

mh... maybe if middlewarePath is different we should the different once 🤔

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 wanted to do something that would allow multiple middlewares on different paths to still get invoked.

Will there ever be a case where there are multiple with different paths? Not sure, maybe not, but that's why I didn't just do a Boolean.

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 was a fix for the same middleware running multiple times, so I feel like considering the path is somewhat important.

Copy link
Member

Choose a reason for hiding this comment

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

yes I was just asking, all of this feels a bit unclear to me in any case 😓

Copy link
Member

@dario-piotrowicz dario-piotrowicz left a comment

Choose a reason for hiding this comment

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

Thanks a lot @james-elicx 🙂

@dario-piotrowicz dario-piotrowicz merged commit 2dceb75 into cloudflare:main Jun 26, 2023
4 checks passed
@james-elicx james-elicx deleted the james/fix-middleware-invocations branch June 26, 2023 14:43
Kebab11noel pushed a commit to Likespace-Kft/next-on-pages that referenced this pull request Aug 20, 2023
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.

None yet

2 participants