Skip to content

Commit

Permalink
Add preconnect for CDN resources
Browse files Browse the repository at this point in the history
  • Loading branch information
CMeeg committed Nov 15, 2023
1 parent 1cfd5bd commit 95a957e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import './globals.css'
import type { Metadata } from 'next'
import { Inter } from 'next/font/google'
import { PreloadResources } from './preload-resources'
import { AppInsightsProvider } from '@/components/instrumentation/AppInsightsProvider'

const inter = Inter({ subsets: ['latin'] })
Expand All @@ -17,6 +18,7 @@ export default function RootLayout({
}) {
return (
<html lang="en">
<PreloadResources />
<body className={inter.className}>
<AppInsightsProvider>{children}</AppInsightsProvider>
</body>
Expand Down
17 changes: 17 additions & 0 deletions src/app/preload-resources.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use client'

import ReactDOM from 'react-dom'
import { getCdnUrl } from '@/lib/url'

function PreloadResources() {
const cdnUrl = getCdnUrl('', false)

if (cdnUrl.length > 0) {
// @ts-ignore
ReactDOM.preconnect(cdnUrl)
}

return null
}

export { PreloadResources }
16 changes: 12 additions & 4 deletions src/lib/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,20 @@ const getAbsoluteUrl = (path?: string) => {
return joinUrlSegments([baseUrl, path])
}

const getCdnUrl = (path: string) => {
if (!baseCdnUrl || !buildId) {
return path
const getCdnUrl = (path?: string, includeFingerprint = true) => {
if (!baseCdnUrl) {
return path ?? ''
}

return joinUrlSegments([baseCdnUrl, buildId, path])
if (!path) {
return baseCdnUrl
}

if (includeFingerprint && buildId) {
return joinUrlSegments([baseCdnUrl, buildId, path])
}

return joinUrlSegments([baseCdnUrl, path])
}

export { getAbsoluteUrl, getCdnUrl }

0 comments on commit 95a957e

Please sign in to comment.