From 293b16ba1e10d18467166cfcf8c98d8018712eb4 Mon Sep 17 00:00:00 2001 From: mac <2099930591@qq.com> Date: Sat, 17 May 2025 13:08:03 +0800 Subject: [PATCH 1/4] fix the external URL detected as local link bug --- packages/gatsby-link/src/is-local-link.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/gatsby-link/src/is-local-link.js b/packages/gatsby-link/src/is-local-link.js index b97c7a1915f5f..030718fd5af7f 100644 --- a/packages/gatsby-link/src/is-local-link.js +++ b/packages/gatsby-link/src/is-local-link.js @@ -2,12 +2,12 @@ const ABSOLUTE_URL_REGEX = /^[a-zA-Z][a-zA-Z\d+\-.]*?:/ const isAbsolute = path => ABSOLUTE_URL_REGEX.test(path) -export const isLocalLink = path => { - if (typeof path !== `string`) { - return undefined - // TODO(v5): Re-Add TypeError - // throw new TypeError(`Expected a \`string\`, got \`${typeof path}\``) +export function isLocalLink(path) { + // Handle null/undefined case + if (!path) throw new TypeError(`Expected a \`string\`, got \`${typeof path}\``) + if (/^(?:[a-z+]+:)?\/\//i.test(path)) { + return false } - - return !isAbsolute(path) + // If it's not a protocol-based URL, it's likely a local link + return true } From 6dad697a3b0b80c48755628be44426d86aadf34a Mon Sep 17 00:00:00 2001 From: mac <2099930591@qq.com> Date: Sat, 17 May 2025 13:36:44 +0800 Subject: [PATCH 2/4] fix the external URL detected as local link bug --- packages/gatsby-link/src/is-local-link.js | 26 +++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/gatsby-link/src/is-local-link.js b/packages/gatsby-link/src/is-local-link.js index 030718fd5af7f..b53d6f1a98213 100644 --- a/packages/gatsby-link/src/is-local-link.js +++ b/packages/gatsby-link/src/is-local-link.js @@ -1,13 +1,21 @@ -// Copied from https://github.com/sindresorhus/is-absolute-url/blob/3ab19cc2e599a03ea691bcb8a4c09fa3ebb5da4f/index.js -const ABSOLUTE_URL_REGEX = /^[a-zA-Z][a-zA-Z\d+\-.]*?:/ -const isAbsolute = path => ABSOLUTE_URL_REGEX.test(path) +// Recognize absolute URLs like https://, http://, mailto:, etc. +const ABSOLUTE_URL_REGEX = /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\//; +/** + * Check if a path is an absolute URL (external). + */ +function isAbsolute(path) { + return ABSOLUTE_URL_REGEX.test(path); +} + +/** + * Check if a path is a local (internal) link. + * Returns true if the path is relative to the site, false otherwise. + */ export function isLocalLink(path) { - // Handle null/undefined case - if (!path) throw new TypeError(`Expected a \`string\`, got \`${typeof path}\``) - if (/^(?:[a-z+]+:)?\/\//i.test(path)) { - return false + if (typeof path !== 'string') { + throw new TypeError(`Expected a string, got ${typeof path}`); } - // If it's not a protocol-based URL, it's likely a local link - return true + + return !isAbsolute(path); } From feae011125af709453fc6cfcb4d763f4f58f02f6 Mon Sep 17 00:00:00 2001 From: mac <2099930591@qq.com> Date: Sat, 17 May 2025 13:40:34 +0800 Subject: [PATCH 3/4] fix the external URL detected as local link bug --- packages/gatsby-link/src/is-local-link.js | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/packages/gatsby-link/src/is-local-link.js b/packages/gatsby-link/src/is-local-link.js index b53d6f1a98213..e5d2d93d2afbb 100644 --- a/packages/gatsby-link/src/is-local-link.js +++ b/packages/gatsby-link/src/is-local-link.js @@ -1,21 +1,13 @@ -// Recognize absolute URLs like https://, http://, mailto:, etc. -const ABSOLUTE_URL_REGEX = /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\//; +const ABSOLUTE_URL_REGEX = /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\// -/** - * Check if a path is an absolute URL (external). - */ function isAbsolute(path) { - return ABSOLUTE_URL_REGEX.test(path); + return ABSOLUTE_URL_REGEX.test(path) } -/** - * Check if a path is a local (internal) link. - * Returns true if the path is relative to the site, false otherwise. - */ export function isLocalLink(path) { - if (typeof path !== 'string') { - throw new TypeError(`Expected a string, got ${typeof path}`); + if (typeof path !== `string`) { + throw new TypeError(`Expected a string, got ${typeof path}`) } - return !isAbsolute(path); + return !isAbsolute(path) } From 8446a32295ac618bfc0722ebb7ee875897154007 Mon Sep 17 00:00:00 2001 From: mac <2099930591@qq.com> Date: Sat, 17 May 2025 22:10:47 +0800 Subject: [PATCH 4/4] fix the external URL detected as local link bug --- packages/gatsby-link/src/is-local-link.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/gatsby-link/src/is-local-link.js b/packages/gatsby-link/src/is-local-link.js index e5d2d93d2afbb..6ac72e3740332 100644 --- a/packages/gatsby-link/src/is-local-link.js +++ b/packages/gatsby-link/src/is-local-link.js @@ -6,8 +6,8 @@ function isAbsolute(path) { export function isLocalLink(path) { if (typeof path !== `string`) { - throw new TypeError(`Expected a string, got ${typeof path}`) + return undefined } - + if (!path) return false return !isAbsolute(path) }