Skip to content

Commit

Permalink
improve code style of string operation in MiddlewareEndpoint (#70780)
Browse files Browse the repository at this point in the history
### Why?

Avoiding allocations
  • Loading branch information
sokra authored Oct 15, 2024
1 parent 7a62bbf commit bff036b
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions crates/next-api/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,29 +183,26 @@ impl MiddlewareEndpoint {
let has_locale = matcher.locale;

if has_i18n_locales && has_locale {
source = format!(
"/:nextInternalLocale((?!_next/)[^/.]{{1,}}){}",
if is_root {
"".to_string()
} else {
source.to_string()
}
);
if is_root {
source.clear();
}
source.insert_str(0, "/:nextInternalLocale((?!_next/)[^/.]{1,})");
}

let last_part = if is_root {
format!(
"({}/?index|/?index\\\\.json)?",
if has_i18n { "|\\\\.json|" } else { "" }
)
if is_root {
source.push('(');
if has_i18n {
source.push_str("|\\\\.json|");
}
source.push_str("/?index|/?index\\\\.json)?")
} else {
"(.json)?".into()
source.push_str("(.json)?")
};

source = format!("/:nextData(_next/data/[^/]{{1,}})?{}{}", source, last_part);
source.insert_str(0, "/:nextData(_next/data/[^/]{1,})?");

if let Some(base_path) = base_path {
source = format!("{}{}", base_path, source);
source.insert_str(0, base_path);
}

// TODO: The implementation of getMiddlewareMatchers outputs a regex here using
Expand Down

0 comments on commit bff036b

Please sign in to comment.