Skip to content

Commit

Permalink
Copy url join code to source code instead of using it from node_modul…
Browse files Browse the repository at this point in the history
…es to fix issue in build
  • Loading branch information
teodorus-nathaniel committed Feb 28, 2024
1 parent 72a14fa commit 9673561
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 13 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@
"sort-keys-recursive": "^2.1.10",
"store": "^2.0.12",
"strip-markdown": "^4.0.0",
"url-join": "^5.0.0",
"url-loader": "^4.1.1",
"use-local-storage": "^3.0.0",
"yup": "^0.32.9",
Expand Down
2 changes: 1 addition & 1 deletion src/components/main/HomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { CreatorDashboardHomeVariant } from '../creators/CreatorDashboardSidebar
import MobileActiveStakingSection from '../creators/MobileActiveStakingSection'
import { ShowLikeablePostsProvider } from '../posts/ShowLikeablePostsContext'
import WriteSomething from '../posts/WriteSomething'
import { useReferralId } from '../referral'
import { useReferralId } from '../referral/ReferralUrlChanger'
import { useIsMobileWidthOrDevice } from '../responsive'
import { CreatorsSpaces } from '../spaces/LatestSpacesPage'
import Section from '../utils/Section'
Expand Down
9 changes: 4 additions & 5 deletions src/components/referral/CustomLink.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import Link, { LinkProps } from 'next/link'
import urlJoin from 'url-join'
import { useReferralId } from '.'
import urlJoin from 'src/utils/url-join'
import { useReferralId } from './ReferralUrlChanger'

export default function CustomLink(props: React.PropsWithChildren<LinkProps>) {
const refId = useReferralId()
if (refId) {
const { href, as } = props
props = {
...props,
href: augmentLink(href, refId),
as: as && augmentLink(as, refId),
href: augmentLink(props.href, refId),
as: props.as && augmentLink(props.as, refId),
}
}

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { GoogleAnalytics } from 'nextjs-google-analytics'
import { DatahubSubscriber } from 'src/components/utils/datahub/subscriber'
import Script from 'next/script'
import { initAllStores } from 'src/stores/registry'
import { ReferralUrlChanger } from 'src/components/referral'
import { ReferralUrlChanger } from 'src/components/referral/ReferralUrlChanger'

dayjs.extend(relativeTime)
dayjs.extend(localizedFormat)
Expand Down
104 changes: 104 additions & 0 deletions src/utils/url-join.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* Code taken from [email protected] package
* If its used via node_modules, it causes some issues with the build
*/

function normalize(strArray: string[]) {
const resultArray = []
if (strArray.length === 0) {
return ''
}

// Filter out any empty string values.
strArray = strArray.filter(part => part !== '')

if (typeof strArray[0] !== 'string') {
throw new TypeError('Url must be a string. Received ' + strArray[0])
}

// If the first part is a plain protocol, we combine it with the next part.
if (strArray[0].match(/^[^/:]+:\/*$/) && strArray.length > 1) {
strArray[0] = strArray.shift() + strArray[0]
}

// If the first part is a leading slash, we combine it with the next part.
if (strArray[0] === '/' && strArray.length > 1) {
strArray[0] = strArray.shift() + strArray[0]
}

// There must be two or three slashes in the file protocol, two slashes in anything else.
if (strArray[0].match(/^file:\/\/\//)) {
strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1:///')
} else if (!strArray[0].match(/^\[.*:.*\]/)) {
// If the first part is not an IPv6 host, we replace the protocol.
strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1://')
}

for (let i = 0; i < strArray.length; i++) {
let component = strArray[i]

if (typeof component !== 'string') {
throw new TypeError('Url must be a string. Received ' + component)
}

if (i > 0) {
// Removing the starting slashes for each component but the first.
component = component.replace(/^[\/]+/, '')
}
if (i < strArray.length - 1) {
// Removing the ending slashes for each component but the last.
component = component.replace(/[\/]+$/, '')
} else {
// For the last component we will combine multiple slashes to a single one.
component = component.replace(/[\/]+$/, '/')
}

if (component === '') {
continue
}

resultArray.push(component)
}

let str = ''

for (let i = 0; i < resultArray.length; i++) {
const part = resultArray[i]

// Do not add a slash if this is the first part.
if (i === 0) {
str += part
continue
}

const prevPart = resultArray[i - 1]

// Do not add a slash if the previous part ends with start of the query param or hash.
if ((prevPart && prevPart.endsWith('?')) || prevPart.endsWith('#')) {
str += part
continue
}

str += '/' + part
}
// Each input component is now separated by a single slash except the possible first plain protocol part.

// remove trailing slash before parameters or hash
str = str.replace(/\/(\?|&|#[^!])/g, '$1')

// replace ? and & in parameters with &
const [beforeHash, afterHash] = str.split('#')
const parts = beforeHash.split(/(?:\?|&)+/).filter(Boolean)
str =
parts.shift() +
(parts.length > 0 ? '?' : '') +
parts.join('&') +
(afterHash && afterHash.length > 0 ? '#' + afterHash : '')

return str
}

export default function urlJoin(...args: string[]) {
const parts = Array.from(Array.isArray(args[0]) ? args[0] : args)
return normalize(parts)
}
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13801,11 +13801,6 @@ urix@^0.1.0:
resolved "https://registry.yarnpkg.com/urix/-/urix-0.1.0.tgz#da937f7a62e21fec1fd18d49b35c2935067a6c72"
integrity sha512-Am1ousAhSLBeB9cG/7k7r2R0zj50uDRlZHPGbazid5s9rlF1F/QKYObEKSIunSjIOkJZqwRRLpvewjEkM7pSqg==

url-join@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/url-join/-/url-join-5.0.0.tgz#c2f1e5cbd95fa91082a93b58a1f42fecb4bdbcf1"
integrity sha512-n2huDr9h9yzd6exQVnH/jU5mr+Pfx08LRXXZhkLLetAMESRj+anQsTAh940iMrIetKAmry9coFuZQ2jY8/p3WA==

url-loader@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/url-loader/-/url-loader-4.1.1.tgz#28505e905cae158cf07c92ca622d7f237e70a4e2"
Expand Down

0 comments on commit 9673561

Please sign in to comment.