Skip to content

Commit

Permalink
Merge branch 'canary' into format-server-error-check-message
Browse files Browse the repository at this point in the history
  • Loading branch information
hanneslund committed Mar 1, 2023
2 parents 3930e5e + 419ade2 commit 780b735
Show file tree
Hide file tree
Showing 23 changed files with 120 additions and 38 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"registry": "https://registry.npmjs.org/"
}
},
"version": "13.2.3-canary.0"
"version": "13.2.3-canary.1"
}
2 changes: 1 addition & 1 deletion packages/create-next-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-next-app",
"version": "13.2.3-canary.0",
"version": "13.2.3-canary.1",
"keywords": [
"react",
"next",
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-config-next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-config-next",
"version": "13.2.3-canary.0",
"version": "13.2.3-canary.1",
"description": "ESLint configuration used by NextJS.",
"main": "index.js",
"license": "MIT",
Expand All @@ -12,7 +12,7 @@
"test-pack": "cd ../../ && pnpm test-pack eslint-config-next"
},
"dependencies": {
"@next/eslint-plugin-next": "13.2.3-canary.0",
"@next/eslint-plugin-next": "13.2.3-canary.1",
"@rushstack/eslint-patch": "^1.1.3",
"@typescript-eslint/parser": "^5.42.0",
"eslint-import-resolver-node": "^0.3.6",
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/eslint-plugin-next",
"version": "13.2.3-canary.0",
"version": "13.2.3-canary.1",
"description": "ESLint plugin for NextJS.",
"main": "dist/index.js",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/font/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/font",
"version": "13.2.3-canary.0",
"version": "13.2.3-canary.1",
"repository": {
"url": "vercel/next.js",
"directory": "packages/font"
Expand Down
2 changes: 1 addition & 1 deletion packages/next-bundle-analyzer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/bundle-analyzer",
"version": "13.2.3-canary.0",
"version": "13.2.3-canary.1",
"main": "index.js",
"types": "index.d.ts",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-codemod/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/codemod",
"version": "13.2.3-canary.0",
"version": "13.2.3-canary.1",
"license": "MIT",
"dependencies": {
"chalk": "4.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-env/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/env",
"version": "13.2.3-canary.0",
"version": "13.2.3-canary.1",
"keywords": [
"react",
"next",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-mdx/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/mdx",
"version": "13.2.3-canary.0",
"version": "13.2.3-canary.1",
"main": "index.js",
"license": "MIT",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/next-plugin-storybook/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/plugin-storybook",
"version": "13.2.3-canary.0",
"version": "13.2.3-canary.1",
"repository": {
"url": "vercel/next.js",
"directory": "packages/next-plugin-storybook"
Expand Down
2 changes: 1 addition & 1 deletion packages/next-polyfill-module/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/polyfill-module",
"version": "13.2.3-canary.0",
"version": "13.2.3-canary.1",
"description": "A standard library polyfill for ES Modules supporting browsers (Edge 16+, Firefox 60+, Chrome 61+, Safari 10.1+)",
"main": "dist/polyfill-module.js",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-polyfill-nomodule/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/polyfill-nomodule",
"version": "13.2.3-canary.0",
"version": "13.2.3-canary.1",
"description": "A polyfill for non-dead, nomodule browsers.",
"main": "dist/polyfill-nomodule.js",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/swc",
"version": "13.2.3-canary.0",
"version": "13.2.3-canary.1",
"private": true,
"scripts": {
"clean": "rm -rf ./native/*",
Expand Down
14 changes: 7 additions & 7 deletions packages/next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next",
"version": "13.2.3-canary.0",
"version": "13.2.3-canary.1",
"description": "The React Framework",
"main": "./dist/server/next.js",
"license": "MIT",
Expand Down Expand Up @@ -80,7 +80,7 @@
]
},
"dependencies": {
"@next/env": "13.2.3-canary.0",
"@next/env": "13.2.3-canary.1",
"@swc/helpers": "0.4.14",
"caniuse-lite": "^1.0.30001406",
"postcss": "8.4.14",
Expand Down Expand Up @@ -135,11 +135,11 @@
"@hapi/accept": "5.0.2",
"@napi-rs/cli": "2.14.7",
"@napi-rs/triples": "1.1.0",
"@next/polyfill-module": "13.2.3-canary.0",
"@next/polyfill-nomodule": "13.2.3-canary.0",
"@next/react-dev-overlay": "13.2.3-canary.0",
"@next/react-refresh-utils": "13.2.3-canary.0",
"@next/swc": "13.2.3-canary.0",
"@next/polyfill-module": "13.2.3-canary.1",
"@next/polyfill-nomodule": "13.2.3-canary.1",
"@next/react-dev-overlay": "13.2.3-canary.1",
"@next/react-refresh-utils": "13.2.3-canary.1",
"@next/swc": "13.2.3-canary.1",
"@opentelemetry/api": "1.4.0",
"@segment/ajv-human-errors": "2.1.2",
"@taskr/clear": "1.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/build/analysis/get-page-static-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ async function tryToReadFile(filePath: string, shouldThrow: boolean) {
}
}

function getMiddlewareMatchers(
export function getMiddlewareMatchers(
matcherOrMatchers: unknown,
nextConfig: NextConfig
): MiddlewareMatcher[] {
Expand Down
69 changes: 64 additions & 5 deletions packages/next/src/server/lib/route-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import { RouteKind } from '../future/route-kind'
import { DefaultRouteMatcherManager } from '../future/route-matcher-managers/default-route-matcher-manager'
import { RouteMatch } from '../future/route-matches/route-match'
import type { PageChecker, Route } from '../router'
import { getMiddlewareMatchers } from '../../build/analysis/get-page-static-info'
import { getMiddlewareRouteMatcher } from '../../shared/lib/router/utils/middleware-route-matcher'
import { join } from 'path'

type MiddlewareConfig = {
matcher: string[]
files: string[]
}
type RouteResult =
| {
type: 'rewrite'
Expand Down Expand Up @@ -48,7 +55,11 @@ class DevRouteMatcherManager extends DefaultRouteMatcherManager {
}
}

export async function makeResolver(dir: string, nextConfig: NextConfig) {
export async function makeResolver(
dir: string,
nextConfig: NextConfig,
middleware: MiddlewareConfig
) {
const url = require('url') as typeof import('url')
const { default: Router } = require('../router') as typeof import('../router')
const { getPathMatch } =
Expand All @@ -65,14 +76,57 @@ export async function makeResolver(dir: string, nextConfig: NextConfig) {
const devServer = new DevServer({
dir,
conf: nextConfig,
hostname: 'localhost',
port: 3000,
})

await devServer.matchers.reload()

// @ts-expect-error
devServer.customRoutes = await loadCustomRoutes(nextConfig)
if (middleware.files?.length) {
// @ts-expect-error
devServer.customRoutes = await loadCustomRoutes(nextConfig)

const matchers = middleware.matcher
? getMiddlewareMatchers(middleware.matcher, nextConfig)
: [{ regexp: '.*' }]
// @ts-expect-error
devServer.middleware = {
page: '/',
match: getMiddlewareRouteMatcher(matchers),
matchers,
}

type GetEdgeFunctionInfo =
typeof DevServer['prototype']['getEdgeFunctionInfo']
const getEdgeFunctionInfo = (
original: GetEdgeFunctionInfo
): GetEdgeFunctionInfo => {
return (params: { page: string; middleware: boolean }) => {
if (params.middleware) {
return {
name: 'middleware',
paths: middleware.files.map((file) => join(process.cwd(), file)),
env: [],
wasm: [],
assets: [],
}
}
return original(params)
}
}
// @ts-expect-error protected
devServer.getEdgeFunctionInfo = getEdgeFunctionInfo(
// @ts-expect-error protected
devServer.getEdgeFunctionInfo.bind(devServer)
)
// @ts-expect-error protected
devServer.hasMiddleware = () => true
}

const routeResults = new WeakMap<any, string>()
const routes = devServer.generateRoutes.bind(devServer)()
const routes = devServer.generateRoutes()
// @ts-expect-error protected
const catchAllMiddleware = devServer.generateCatchAllMiddlewareRoute(true)

routes.matchers = new DevRouteMatcherManager(
// @ts-expect-error internal method
Expand All @@ -81,6 +135,7 @@ export async function makeResolver(dir: string, nextConfig: NextConfig) {

const router = new Router({
...routes,
catchAllMiddleware,
catchAllRoute: {
match: getPathMatch('/:path*'),
name: 'catchall route',
Expand Down Expand Up @@ -112,6 +167,7 @@ export async function makeResolver(dir: string, nextConfig: NextConfig) {
route.type === 'redirect' ||
route.type === 'header' ||
route.name === 'catchall route' ||
route.name === 'middleware catchall' ||
route.name?.includes('check')
return matches
})
Expand All @@ -122,9 +178,12 @@ export async function makeResolver(dir: string, nextConfig: NextConfig) {
) {
const req = new NodeNextRequest(_req)
const res = new NodeNextResponse(_res)
const parsedUrl = url.parse(req.url!, true)
// @ts-expect-error protected
devServer.attachRequestMeta(req, parsedUrl)
;(req as any)._initUrl = req.url

await router.execute.bind(router)(req, res, url.parse(req.url!, true))
await router.execute(req, res, parsedUrl)

if (!res.originalResponse.headersSent) {
res.setHeader('x-nextjs-route-result', '1')
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/shared/lib/router/utils/resolve-href.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function resolveHref(

if ((urlParts[0] || '').match(/(\/\/|\\)/)) {
console.error(
`Invalid href passed to next/router: ${urlAsString}, repeated forward-slashes (//) or backslashes \\ are not valid in the href`
`Invalid href '${urlAsString}' passed to next/router in page: '${router.pathname}'. Repeated forward-slashes (//) or backslashes \\ are not valid in the href.`
)
const normalizedUrl = normalizeRepeatedSlashes(urlAsStringNoProto)
urlAsString = (urlProtoMatch ? urlProtoMatch[0] : '') + normalizedUrl
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dev-overlay/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/react-dev-overlay",
"version": "13.2.3-canary.0",
"version": "13.2.3-canary.1",
"description": "A development-only overlay for developing React applications.",
"repository": {
"url": "vercel/next.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-refresh-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@next/react-refresh-utils",
"version": "13.2.3-canary.0",
"version": "13.2.3-canary.1",
"description": "An experimental package providing utilities for React Refresh.",
"repository": {
"url": "vercel/next.js",
Expand Down
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Link from 'next/link'

export default function Page() {
return <Link href="/hello//world">Hello</Link>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createNextDescribe } from 'e2e-utils'
import { check } from 'next-test-utils'

createNextDescribe(
'repeated-forward-slashes-error',
{
files: __dirname,
},
({ next }) => {
it('should log error when href has repeated forward-slashes', async () => {
await next.render$('/my/path/name')
await check(() => next.cliOutput, /Invalid href/)
expect(next.cliOutput).toContain(
"Invalid href '/hello//world' passed to next/router in page: '/my/path/[name]'. Repeated forward-slashes (//) or backslashes \\ are not valid in the href."
)
})
}
)
2 changes: 1 addition & 1 deletion test/integration/repeated-slashes/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) {
browser.eval(
'window.caughtErrors.map(err => typeof err !== "string" ? err.message : err).join(", ")'
),
new RegExp(escapeRegex(`Invalid href passed to next/router: ${href}`))
new RegExp(escapeRegex(`Invalid href '${href}'`))
)
}
})
Expand Down

0 comments on commit 780b735

Please sign in to comment.