Unverified Commit c251de87 authored by OJ Kwon's avatar OJ Kwon Committed by GitHub

feat(next-core): support parsing matcher config object (#64678)

### What

- closes #63896

PR implements parsing JSValue for the matcher config if given item is an
object. We had those types already declared in place but somehow parsing
ignores it.
parent 63701552
...@@ -5,7 +5,7 @@ use next_core::{ ...@@ -5,7 +5,7 @@ use next_core::{
next_edge::entry::wrap_edge_entry, next_edge::entry::wrap_edge_entry,
next_manifests::{EdgeFunctionDefinition, MiddlewareMatcher, MiddlewaresManifestV2}, next_manifests::{EdgeFunctionDefinition, MiddlewareMatcher, MiddlewaresManifestV2},
next_server::{get_server_runtime_entries, ServerContextType}, next_server::{get_server_runtime_entries, ServerContextType},
util::parse_config_from_source, util::{parse_config_from_source, MiddlewareMatcherKind},
}; };
use tracing::Instrument; use tracing::Instrument;
use turbo_tasks::{Completion, Value, Vc}; use turbo_tasks::{Completion, Value, Vc};
...@@ -138,9 +138,12 @@ impl MiddlewareEndpoint { ...@@ -138,9 +138,12 @@ impl MiddlewareEndpoint {
let matchers = if let Some(matchers) = config.await?.matcher.as_ref() { let matchers = if let Some(matchers) = config.await?.matcher.as_ref() {
matchers matchers
.iter() .iter()
.map(|matcher| MiddlewareMatcher { .map(|matcher| match matcher {
original_source: matcher.to_string(), MiddlewareMatcherKind::Str(matchers) => MiddlewareMatcher {
..Default::default() original_source: matchers.to_string(),
..Default::default()
},
MiddlewareMatcherKind::Matcher(matcher) => matcher.clone(),
}) })
.collect() .collect()
} else { } else {
......
...@@ -4,7 +4,7 @@ use anyhow::{bail, Context, Result}; ...@@ -4,7 +4,7 @@ use anyhow::{bail, Context, Result};
use indexmap::IndexMap; use indexmap::IndexMap;
use serde::{Deserialize, Deserializer, Serialize}; use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value as JsonValue; use serde_json::Value as JsonValue;
use turbo_tasks::{trace::TraceRawVcs, Vc}; use turbo_tasks::{trace::TraceRawVcs, TaskInput, Vc};
use turbopack_binding::{ use turbopack_binding::{
turbo::{tasks_env::EnvMap, tasks_fs::FileSystemPath}, turbo::{tasks_env::EnvMap, tasks_fs::FileSystemPath},
turbopack::{ turbopack::{
...@@ -197,7 +197,19 @@ pub enum OutputType { ...@@ -197,7 +197,19 @@ pub enum OutputType {
Export, Export,
} }
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)] #[derive(
Debug,
Clone,
Hash,
Eq,
PartialEq,
Ord,
PartialOrd,
TaskInput,
TraceRawVcs,
Serialize,
Deserialize,
)]
#[serde(tag = "type", rename_all = "kebab-case")] #[serde(tag = "type", rename_all = "kebab-case")]
pub enum RouteHas { pub enum RouteHas {
Header { Header {
......
...@@ -8,7 +8,7 @@ use indexmap::IndexSet; ...@@ -8,7 +8,7 @@ use indexmap::IndexSet;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use turbo_tasks::{trace::TraceRawVcs, TaskInput}; use turbo_tasks::{trace::TraceRawVcs, TaskInput};
use crate::next_config::{CrossOriginConfig, Rewrites}; use crate::next_config::{CrossOriginConfig, Rewrites, RouteHas};
#[derive(Serialize, Default, Debug)] #[derive(Serialize, Default, Debug)]
pub struct PagesManifest { pub struct PagesManifest {
...@@ -44,30 +44,20 @@ impl Default for MiddlewaresManifest { ...@@ -44,30 +44,20 @@ impl Default for MiddlewaresManifest {
} }
} }
#[derive(Serialize, Debug)] #[derive(
#[serde(tag = "type", rename_all = "kebab-case")] Debug,
pub enum RouteHas { Clone,
Header { Hash,
key: String, Eq,
#[serde(skip_serializing_if = "Option::is_none")] PartialEq,
value: Option<String>, Ord,
}, PartialOrd,
Cookie { TaskInput,
key: String, TraceRawVcs,
#[serde(skip_serializing_if = "Option::is_none")] Serialize,
value: Option<String>, Deserialize,
}, Default,
Query { )]
key: String,
#[serde(skip_serializing_if = "Option::is_none")]
value: Option<String>,
},
Host {
value: String,
},
}
#[derive(Serialize, Default, Debug)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct MiddlewareMatcher { pub struct MiddlewareMatcher {
// When skipped next.js with fill that during merging. // When skipped next.js with fill that during merging.
......
...@@ -77,5 +77,13 @@ export const config = { ...@@ -77,5 +77,13 @@ export const config = {
}, },
], ],
}, },
{
source:
'/((?!api|monitoring|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt|manifest|icon|source-match|has-match-1|has-match-2|has-match-3|has-match-4|has-match-5|missing-match-1|missing-match-2|routes).*)',
missing: [
{ type: 'header', key: 'next-router-prefetch' },
{ type: 'header', key: 'purpose', value: 'prefetch' },
],
},
], ],
} }
...@@ -31,6 +31,16 @@ describe('Middleware custom matchers', () => { ...@@ -31,6 +31,16 @@ describe('Middleware custom matchers', () => {
}, },
}) })
expect(res2.headers.get('x-from-middleware')).toBeFalsy() expect(res2.headers.get('x-from-middleware')).toBeFalsy()
const res3 = await fetchViaHTTP(next.url, '/')
expect(res3.headers.get('x-from-middleware')).toBeDefined()
const res4 = await fetchViaHTTP(next.url, '/', undefined, {
headers: {
purpose: 'prefetch',
},
})
expect(res4.headers.get('x-from-middleware')).toBeFalsy()
}) })
it('should match missing query correctly', async () => { it('should match missing query correctly', async () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment