rsnext/packages/next/server/web
Javi Velasco e52bee37af
Refactor sandbox module cache (#31822)
To run middleware we are using a **sandbox** that emulates the web runtime and keeps a module cache. This cache is shared for all of the modules that we run using the sandbox while there are some module-level APIs that must be scoped depending on the module we are running.

One example of this is `fetch` where we want to always inject a special header that indicate the module that is performing the fetch and use it to avoid getting into infinite loops for middleware. For those cases the cached implementation will be the first one that instantiates the module and therefore we can actually get into infinite loops. This is the reason why #31800 is failing.

With this PR we refactor the sandbox so that the module cache is scoped per module name. This means that one execution of a middleware will preserve its cache only for that module so that each execution will still have its own `fetch` implementation, fixing this issue. Also, with this refactor the code is more clear and we also provide an option to avoid using the cache.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
2021-11-26 12:06:41 +00:00
..
sandbox Refactor sandbox module cache (#31822) 2021-11-26 12:06:41 +00:00
spec-compliant middleware: add request referrer support (#31343) 2021-11-15 19:52:44 +00:00
spec-extension avoid mutating response.cookie options (#31679) 2021-11-22 11:20:01 +00:00
adapter.ts Improve deprecation errors for new middleware API (#30316) 2021-10-26 17:03:39 +02:00
error.ts Improve deprecation errors for new middleware API (#30316) 2021-10-26 17:03:39 +02:00
form-data.ts Implement Middleware RFC (#30081) 2021-10-20 17:52:11 +00:00
is.ts Implement Middleware RFC (#30081) 2021-10-20 17:52:11 +00:00
next-url.ts fix(middleware): consider localhost variations (#31603) 2021-11-19 21:24:22 +00:00
render.ts Add new target for middleware (#30299) 2021-10-26 18:50:56 +02:00
types.ts Add types for geo lat and long (#31624) 2021-11-19 17:09:52 +00:00
utils.ts Split Set-Cookie header correctly (#30560) 2021-10-28 17:46:58 +00:00