MCPcopy
hub / github.com/vercel/next.js / initialize

Function initialize

packages/next/src/client/index.tsx:190–290  ·  view source on GitHub ↗
(opts: { devClient?: any } = {})

Source from the content-addressed store, hash-verified

188}
189
190export async function initialize(opts: { devClient?: any } = {}): Promise<{
191 assetPrefix: string
192}> {
193 // This makes sure this specific lines are removed in production
194 if (process.env.NODE_ENV === 'development') {
195 tracer.onSpanEnd(
196 (
197 require('./tracing/report-to-socket') as typeof import('./tracing/report-to-socket')
198 ).default
199 )
200 devClient = opts.devClient
201 }
202
203 initialData = JSON.parse(
204 document.getElementById('__NEXT_DATA__')!.textContent!
205 )
206 window.__NEXT_DATA__ = initialData
207
208 defaultLocale = initialData.defaultLocale
209 const prefix: string = initialData.assetPrefix || ''
210 // With dynamic assetPrefix it's no longer possible to set assetPrefix at the build time
211 // So, this is how we do it in the client side at runtime
212 ;(self as any).__next_set_public_path__(`${prefix}/_next/`)
213
214 asPath = getURL()
215
216 // make sure not to attempt stripping basePath for 404s
217 if (hasBasePath(asPath)) {
218 asPath = removeBasePath(asPath)
219 }
220
221 if (process.env.__NEXT_I18N_SUPPORT) {
222 const { normalizeLocalePath } =
223 require('../shared/lib/i18n/normalize-locale-path') as typeof import('../shared/lib/i18n/normalize-locale-path')
224
225 const { detectDomainLocale } =
226 require('../shared/lib/i18n/detect-domain-locale') as typeof import('../shared/lib/i18n/detect-domain-locale')
227
228 const { parseRelativeUrl } =
229 require('../shared/lib/router/utils/parse-relative-url') as typeof import('../shared/lib/router/utils/parse-relative-url')
230
231 const { formatUrl } =
232 require('../shared/lib/router/utils/format-url') as typeof import('../shared/lib/router/utils/format-url')
233
234 if (initialData.locales) {
235 const parsedAs = parseRelativeUrl(asPath)
236 const localePathResult = normalizeLocalePath(
237 parsedAs.pathname,
238 initialData.locales
239 )
240
241 if (localePathResult.detectedLocale) {
242 parsedAs.pathname = localePathResult.pathname
243 asPath = formatUrl(parsedAs)
244 } else {
245 // derive the default locale if it wasn't detected in the asPath
246 // since we don't prerender static pages with all possible default
247 // locales

Callers 4

next-dev.tsFile · 0.70
next.tsFile · 0.70
next-turbopack.tsFile · 0.70

Calls 15

getURLFunction · 0.90
hasBasePathFunction · 0.90
removeBasePathFunction · 0.90
parseRelativeUrlFunction · 0.85
formatUrlFunction · 0.85
initScriptLoaderFunction · 0.85
initHeadManagerFunction · 0.85
onSpanEndMethod · 0.80
normalizeLocalePathFunction · 0.70
detectDomainLocaleFunction · 0.70
registerFunction · 0.70
requireFunction · 0.50

Tested by

no test coverage detected