rsnext/errors
Javi Velasco e5dee17f77
Enforce absolute URLs in Edge Functions runtime (#33410)
We currently have inconsistencies when working with URLs in the Edge Functions runtime, this PR addresses them introducing a warning for inconsistent usage that will break in the future. Here is the reasoning.

### The Browser

When we are in a browser environment there is a fixed location stored at `globalThis.location`. Then, if one tries to build a request with a relative URL it will work using that location global hostname as _base_ to construct its URL. For example:

```typescript
// https://nextjs.org
new Request('/test').url; // https://nextjs.org/test
Response.redirect('/test').headers.get('Location'); // https://nextjs.org/test
```

However, if we attempt to run the same code from `about:blank` it would not work because the global to use as a base `String(globalThis.location)` is not a valid URL. Therefore a call to `Response.redirect('/test')` or `new Response('/test')` would fail.

### Edge Functions Runtime

In Next.js Edge Functions runtime the situation is slightly different from a browser. Say that we have a root middleware (`pages/_middleware`) that gets invoked for every page. In the middleware file we expose the handler function and also define a global variable that we mutate on every request:

```typescript
// pages/_middleware

let count = 0;

export function middleware(req: NextRequest) {
  console.log(req.url);
  count += 1;
}
```

Currently we cache the module scope in the runtime so subsequent invocations would hold the same globals and the module would not be evaluated again. This would make the counter to increment for each request that the middleware handles. It is for this reason that we **can't have a global location** that changes across different invocations. Each invocation of the same function uses the same global which also holds primitives like `URL` or `Request` so changing an hypothetical `globalThis.location` per request would affect concurrent requests being handled.

Then, it is not possible to use relative URLs in the same way the browser does because we don't have a global to rely on to use its host to compose a URL from a relative path.

### Why it works today

We are **not** validating what is provided to, for example, `NextResponse.rewrite()` nor `NextResponse.redirect()`. We simply create a `Response` instance that adds the corresponding header for the rewrite or the redirect. Then it is **the consumer** the one that composes the final destination based on the request. Theoretically you can pass any value and it would fail on redirect but won't validate the input.

Of course this is inconsistent because it doesn't make sense that `NextResponse.rewrite('/test')` works but `fetch(new NextRequest('/test'))` does not. Also we should validate what is provided. Finally, we want to be consistent with the way a browser behaves so `new Request('/test')` _should_ not work if there is no global location which we lack.

### What this PR does

We will have to deprecate the usage of relative URLs in the previously mentioned scenarios. In preparation for it, this PR adds a validation function in those places where it will break in the future, printing a warning with a link that points to a Next.js page with an explanation of the issue and ways to fix it.

Although middleware changes are not covered by semver, we will roll this for some time to make people aware that this change is coming. Then after a reasonable period of time we can remove the warning and make the code fail when using relative URLs in the previously exposed scenarios.
2022-01-19 15:10:25 +00:00
..
404-get-initial-props.md Rename getServerProps to getServerSideProps (#10722) 2020-02-27 11:04:30 -06:00
amp-bind-jsx-alt.md Add error message for AMP bind syntax (#6865) 2019-04-02 17:32:07 -05:00
amp-export-validation.md Add AMP validation on export (#6794) 2019-03-26 16:21:27 -05:00
api-routes-body-size-limit.md 5MB -> 4MB body size limit (#26887) 2021-07-02 23:17:56 +00:00
api-routes-static-export.md chore: Enable Alex documentation linting for error pages (#26526) 2021-06-23 23:33:58 -05:00
app-container-deprecated.md Prettier fix (#8754) 2019-09-16 15:24:42 +02:00
beta-middleware.md Replace middleware flag with warning (#30160) 2021-10-22 06:40:57 +00:00
build-dir-not-writeable.md Update redirected or broken Vercel docs links. (#21119) 2021-01-14 15:37:21 +00:00
built-in-css-disabled.md Add warning when built-in CSS/SCSS support is disabled (#10942) 2020-03-10 14:29:40 -04:00
can-not-output-to-public.md chore: Enable Alex documentation linting for error pages (#26526) 2021-06-23 23:33:58 -05:00
can-not-output-to-static.md chore: Enable Alex documentation linting for error pages (#26526) 2021-06-23 23:33:58 -05:00
cant-override-next-props.md Update references to zeit/next.js (#13463) 2020-05-27 17:51:11 -04:00
circular-structure.md Remove trailing spaces (#6547) 2019-03-07 17:40:08 +01:00
client-side-exception-occurred.md Update client-side default error (#25997) 2021-06-11 16:23:45 +02:00
config-resolve-alias.md Update references to zeit/next.js (#13463) 2020-05-27 17:51:11 -04:00
conflicting-amp-tag.md Move syntax formatting to prettier (#7454) 2019-05-29 13:57:26 +02:00
conflicting-public-file-page.md docs: update links to docs site (#14305) 2020-06-18 09:54:07 +00:00
conflicting-ssg-paths.md Add proper error when conflicting paths are detected (#20918) 2021-01-11 20:50:17 +00:00
css-global.md Updated the Error message describing the alternative and added styles… (#14652) 2021-07-25 04:53:33 +00:00
css-modules-npm.md Update references to zeit/next.js (#13463) 2020-05-27 17:51:11 -04:00
css-npm.md docs(errors): CSS Imported by a Dependency (#10032) 2020-01-10 10:40:28 -05:00
custom-document-image-import.md Fix incorrect _document.js error when disableStaticImages: true (#30768) 2021-11-02 20:25:12 +00:00
custom-error-no-custom-404.md Warn when user has pages/_error but no pages/404 (#11603) 2020-04-05 13:19:14 +02:00
deprecated-target-config.md Fix link to documentation from deprecated target config message (#30607) 2021-10-29 09:18:25 +00:00
doc-crossorigin-deprecated.md Update references to zeit/next.js (#13463) 2020-05-27 17:51:11 -04:00
duplicate-sass.md Warn on duplicate Sass deps (#16398) 2020-08-20 16:05:29 +00:00
empty-configuration.md fix: 9919 Add warning when no config is exported from next.con… (#10228) 2020-01-29 09:12:30 +01:00
empty-object-getInitialProps.md fix broken link in error page (#10159) 2020-01-19 16:02:44 +01:00
env-key-not-allowed.md Block Certain Env Keys That Are Used Internally (#6260) 2019-02-15 17:49:40 +01:00
env-loading-disabled.md Enable .env support by default (#12911) 2020-05-15 14:02:16 -05:00
experimental-jest-transformer.md Add next-swc jest transform (#30993) 2021-11-08 17:35:04 +01:00
export-all-in-page.md fix typo in export-all-in-page (#15925) 2020-08-06 00:11:11 +00:00
export-image-api.md Update links in next export + next/image error message (#33317) 2022-01-14 15:19:52 +00:00
export-no-custom-routes.md Add warning when exporting with custom routes (#17538) 2020-10-05 07:11:06 +00:00
export-path-mismatch.md docs: update links to docs site (#14305) 2020-06-18 09:54:07 +00:00
failed-loading-swc.md Add note about no-optional to swc error doc (#30566) 2021-10-28 14:04:20 -05:00
future-webpack5-moved-to-webpack5.md chore: Enable Alex documentation linting for error pages (#26526) 2021-06-23 23:33:58 -05:00
generatebuildid-not-a-string.md Trim buildId returned from generateBuildId (#5634) 2018-11-12 15:57:48 +01:00
get-initial-props-as-an-instance-method.md Refactor data fetching API docs (#30615) 2022-01-12 08:56:51 +01:00
google-font-display.md ESLint Plugin: Google Font rules (#24766) 2021-05-10 10:03:28 +02:00
google-font-preconnect.md ESLint Plugin: Google Font rules (#24766) 2021-05-10 10:03:28 +02:00
gsp-redirect-during-prerender.md Add handling for redirects from getStaticProps/getServerSideProps (#16642) 2020-09-08 09:23:21 +02:00
gssp-component-member.md Fix typo in error markdown (#20825) 2021-01-07 10:22:43 +01:00
gssp-export.md Update gssp-export Error (#15529) 2020-07-27 18:47:00 +00:00
gssp-mixed-not-found-redirect.md Add test and errsh for mixed notFound/redirect error (#19076) 2020-11-11 22:02:11 +00:00
gssp-no-mutating-res.md Warn when mutating res if not streaming (#30284) 2021-10-25 20:18:03 -05:00
head-build-id.md Update instances of Now to Vercel where applicable (#13760) 2020-06-04 14:57:17 +00:00
href-interpolation-failed.md Add error when href interpolation fails (#16946) 2020-09-10 20:05:02 +00:00
import-esm-externals.md add support for esm externals (#27069) 2021-07-10 16:49:02 +00:00
improper-devtool.md Warn/revert custom devtool in development mode (#14285) 2020-06-24 04:15:57 +00:00
incompatible-href-as.md Update error when internal href and external as are used (#20658) 2021-01-01 12:14:42 -06:00
inline-script-id.md Add eslint rule for id attribute on inline next/script (#27853) 2021-08-19 20:08:04 +00:00
install-sass.md Improve Sass Error (#10982) 2020-03-11 16:34:14 -04:00
install-sharp.md Update install-sharp.md (#18508) 2020-10-30 10:56:11 -04:00
invalid-api-status-body.md Fix handling for 204 status code with a body (#28479) 2021-08-25 15:52:43 +00:00
invalid-assetprefix.md Add check for invalid assetPrefix (#9759) 2019-12-16 16:07:03 +01:00
invalid-dynamic-options-type.md SWC: Add error checks and tests to next-dynamic (#31683) 2021-11-26 14:22:28 +00:00
invalid-dynamic-suspense.md Document usage of suspense option of next/dynamic (#28210) 2021-08-18 01:21:22 +00:00
invalid-external-rewrite.md Disallow basePath: false for internal routes (#15837) 2020-08-04 17:50:09 +00:00
invalid-getstaticpaths-value.md Update invalid-getstaticpaths-value.md (#29397) 2021-09-26 07:13:13 -05:00
invalid-getstaticprops-value.md feat: add type to previewData (#21574) 2021-04-20 18:13:48 +00:00
invalid-href-passed.md Fix invalid href error message (#17183) 2020-09-18 15:50:02 +00:00
invalid-i18n-config.md Document i18n locale limit (#27542) 2021-07-27 20:09:36 +00:00
invalid-images-config.md Change AVIF to opt-in via configuration (#30180) 2021-10-22 21:08:03 +00:00
invalid-multi-match.md Add err.sh for invalid multi-match usage (#9498) 2019-11-23 02:23:01 +01:00
invalid-page-config.md Handle cases where config is exported after its declaration (#16211) 2020-08-17 17:24:18 +00:00
invalid-project-dir-casing.md Fix invalid project dir casing breaking Next.js on Windows (#29205) 2021-09-22 21:29:27 +00:00
invalid-react-version.md Add checking of react versions (#6892) 2019-04-04 16:47:17 -05:00
invalid-redirect-gssp.md docs: fix missing comma in invalid redirect gssp (#18580) 2020-10-31 23:37:06 -04:00
invalid-relative-url-external-as.md Update error when internal href and external as are used (#20658) 2021-01-01 12:14:42 -06:00
invalid-resolve-alias.md Update references to zeit/next.js (#13463) 2020-05-27 17:51:11 -04:00
invalid-route-source.md [Fix] common misspelling errors (#15288) 2020-07-19 04:38:20 +00:00
invalid-server-options.md Detailed error and warnings upon next() call (#13539) 2020-06-03 03:19:29 +00:00
invalid-styled-jsx-children.md next-swc: styled-jsx error checking and reporting updated (invalid-styled-jsx-children.md) (#31940) 2021-12-20 16:17:37 +01:00
invalid-webpack-5-version.md use webpack resolve api to resolve in externals config (#21205) 2021-01-17 20:02:20 +01:00
large-page-data.md Add warning for large amount of page data (#29956) 2021-10-25 21:07:05 +02:00
link-multiple-children.md Add helpful error for link with multiple children (#25657) 2021-05-31 19:41:57 +00:00
link-passhref.md ESLint Plugin: passHref is not assigned (#24670) 2021-05-10 18:35:11 +00:00
manifest.json Enforce absolute URLs in Edge Functions runtime (#33410) 2022-01-19 15:10:25 +00:00
max-custom-routes-reached.md Add warning for large number of routes (#27214) 2021-07-16 14:59:54 +00:00
middleware-new-signature.md Improve deprecation errors for new middleware API (#30316) 2021-10-26 17:03:39 +02:00
middleware-relative-urls.md Enforce absolute URLs in Edge Functions runtime (#33410) 2022-01-19 15:10:25 +00:00
minification-disabled.md Prettier fix (#8754) 2019-09-16 15:24:42 +02:00
missing-document-component.md Improve Document Component Error (#17047) 2020-09-12 13:39:16 -04:00
missing-env-value.md Add initial process.env stubbing for new env support (#11893) 2020-04-15 02:42:37 -04:00
module-not-found.md Add Import trace for requested module when it fails to resolve (#27840) 2021-08-09 14:38:26 +02:00
multi-tabs.md Move syntax formatting to prettier (#7454) 2019-05-29 13:57:26 +02:00
nested-reserved-page.md Add warning when reserved pages are nested (#13449) 2020-05-27 16:45:53 +00:00
next-config-error.md Ensure error message prints next.config.mjs (#30152) 2021-10-21 23:04:40 +00:00
next-dynamic-modules.md Upgrade to Prettier 2 (#13061) 2020-05-18 15:24:37 -04:00
next-export-no-build-id.md Clear up production build missing message for next start and next export (#19777) 2020-12-08 15:16:56 +00:00
next-export-serverless.md Move syntax formatting to prettier (#7454) 2019-05-29 13:57:26 +02:00
next-head-count-missing.md Revert "Remove next-head-count (#16758)" (#18713) 2020-11-10 16:35:47 -05:00
next-image-missing-loader-width.md Rename next/image dangerously-unoptimized to custom and warn when applicable (#26998) 2021-07-08 19:35:19 +00:00
next-image-missing-loader.md Rename next/image dangerously-unoptimized to custom and warn when applicable (#26998) 2021-07-08 19:35:19 +00:00
next-image-unconfigured-host.md Update next-image-unconfigured-host.md (#24953) 2021-07-19 19:40:52 -05:00
next-script-for-ga.md chore(examples): switch to Script component on the google analytics example (#27674) 2021-09-08 21:03:23 +00:00
next-start-serverless.md chore: Enable Alex documentation linting for error pages (#26526) 2021-06-23 23:33:58 -05:00
no-cache.md Update documentation to fix github cache key (#29983) 2021-10-18 22:19:22 +00:00
no-css-tags.md Adds ESLint with default rule-set (#23702) 2021-04-30 11:09:07 +00:00
no-document-import-in-page.md ESLint Plugin: Fix Document and Head import rules (#25730) 2021-06-07 12:40:28 +02:00
no-document-title.md Add warning when viewport meta tag is added to _document.js (#13452) 2020-07-07 01:43:16 -04:00
no-document-viewport-meta.md Replace zeit/next.js with vercel/next.js (#20849) 2021-01-07 08:41:04 -05:00
no-duplicate-head.md adds no duplicate head rule (#27179) 2021-07-15 13:04:17 -05:00
no-head-element.md [ESLint] Adds lint rule to flag usage of <head> (#32897) 2021-12-31 05:09:44 +00:00
no-head-import-in-document.md ESLint Plugin: Prevent bad imports of next/document and next/head (#24832) 2021-05-10 21:28:06 +00:00
no-html-link-for-pages.md Add docs for ESLint plugin settings and rule options (#28059) 2021-08-19 09:07:30 +00:00
no-img-element.md Fix minor typo in no-img-element.md (#29027) 2021-09-12 00:39:43 +00:00
no-on-app-updated-hook.md Update references to zeit/next.js (#13463) 2020-05-27 17:51:11 -04:00
no-page-custom-font.md fix: typo (#32820) 2021-12-26 13:08:19 +01:00
no-router-instance.md expanded on No Router Instance (#21248) 2021-01-26 10:27:53 +00:00
no-script-in-document-page.md [ESLint] Disallow <Script /> inside _document.js & <Script /> inside the next/head component (#27257) 2021-08-13 23:16:15 +00:00
no-script-in-head-component.md [ESLint] Disallow <Script /> inside _document.js & <Script /> inside the next/head component (#27257) 2021-08-13 23:16:15 +00:00
no-server-import-in-page.md Fix error page doc for no server import in page (#32164) 2021-12-07 18:24:27 +01:00
no-sync-scripts.md Doc "no-sync-scripts" remove experimental notice (#26225) 2021-06-17 08:19:26 +00:00
no-title-in-document-head.md ESLint Plugin: Disallow <title> in Head from next/document (#24868) 2021-05-07 08:49:37 +00:00
no-unwanted-polyfillio.md Adds ESLint with default rule-set (#23702) 2021-04-30 11:09:07 +00:00
non-dynamic-getstaticpaths-usage.md Add error for getStaticPaths on non-dynamic page (#24188) 2021-04-18 09:31:40 +00:00
non-standard-node-env.md Better NODE_ENV explanation (#13476) 2020-05-28 09:10:22 +00:00
opt-out-auto-static-optimization.md docs: update links to docs site (#14305) 2020-06-18 09:54:07 +00:00
opt-out-automatic-prerendering.md docs: update links to docs site (#14305) 2020-06-18 09:54:07 +00:00
page-data-collection-timeout.md docs: remove mention of experimental pageDataCollectionTimeout (#30107) 2021-10-20 19:35:32 +00:00
page-without-valid-component.md fixed typo in page-without-valid-component.md (#15022) 2020-07-10 18:44:17 +00:00
placeholder-blur-data-url.md Add support for AVIF to next/image (#29683) 2021-10-11 23:17:47 +00:00
popstate-state-empty.md Remove unused error (#15748) 2020-07-31 17:29:09 +00:00
postcss-function.md PostCSS Error When Exporting Function (#10242) 2020-01-23 15:39:50 -05:00
postcss-ignored-plugin.md docs: use descriptive links instead of "click here" (#25897) 2021-06-09 13:23:16 -05:00
postcss-shape.md docs: use descriptive links instead of "click here" (#25897) 2021-06-09 13:23:16 -05:00
prefetch-true-deprecated.md chore: Enable Alex documentation linting for error pages (#26526) 2021-06-23 23:33:58 -05:00
prerender-error.md docs: fix url (#33409) 2022-01-18 00:26:24 +01:00
production-start-no-build-id.md Clear up production build missing message for next start and next export (#19777) 2020-12-08 15:16:56 +00:00
promise-in-next-config.md Upgrade to Prettier 2 (#13061) 2020-05-18 15:24:37 -04:00
public-next-folder-conflict.md Match public files priority in dev (#8641) 2019-09-16 23:06:30 +02:00
react-hydration-error.md Fixed syntax error in the example of React Hydration Error (#32773) 2021-12-23 19:25:11 +00:00
react-version.md Update react-version.md (#26093) 2021-06-14 20:24:36 +00:00
render-no-starting-slash.md Add warning when a page is rendered without a starting / (#11418) 2020-04-06 11:54:42 -05:00
reserved-page-prop.md Update url prop handling for pages with new data methods (#10653) 2020-02-26 13:26:55 -05:00
rewrite-auto-export-fallback.md De-experimentalize custom-routes (#14602) 2020-06-27 09:18:18 +00:00
routes-must-be-array.md De-experimentalize custom-routes (#14602) 2020-06-27 09:18:18 +00:00
sharp-missing-in-production.md Add support for AVIF to next/image (#29683) 2021-10-11 23:17:47 +00:00
sharp-version-avif.md Add support for AVIF to next/image (#29683) 2021-10-11 23:17:47 +00:00
ssg-fallback-true-export.md Add error when exporting pages with fallback: true (#13063) 2020-05-19 09:29:34 -04:00
static-dir-deprecated.md chore: Enable Alex documentation linting for error pages (#26526) 2021-06-23 23:33:58 -05:00
static-page-generation-timeout.md Enable shared worker pool by default (#30110) 2021-10-20 14:06:43 -05:00
swc-disabled.md Enable SWC by default when there is no custom Babel config (#29811) 2021-10-23 10:21:44 +02:00
swc-minify-enabled.md Move swcMinify out of experimental (#29810) 2021-10-25 13:49:11 +02:00
template.txt Add util for generating new tests/error documents (#33001) 2022-01-06 09:45:04 -06:00
threw-undefined.md Add util for normalizing errors (#33159) 2022-01-11 14:40:03 -06:00
undefined-webpack-config.md Error Message Clarity (#16052) 2020-12-01 15:58:52 -06:00
url-deprecated.md Remove unsupported examples (#26075) 2021-06-14 20:27:06 +02:00
webpack5.md Change using-preact example dependencies and docs (#30394) 2021-12-11 15:19:49 +01:00