{ // 获取包含Hugging Face文本的span元素 const spans = link.querySelectorAll('span.whitespace-nowrap, span.hidden.whitespace-nowrap'); spans.forEach(span => { if (span.textContent && span.textContent.trim().match(/Hugging\s*Face/i)) { span.textContent = 'AI快站'; } }); }); // 替换logo图片的alt属性 document.querySelectorAll('img[alt*="Hugging"], img[alt*="Face"]').forEach(img => { if (img.alt.match(/Hugging\s*Face/i)) { img.alt = 'AI快站 logo'; } }); } // 替换导航栏中的链接 function replaceNavigationLinks() { // 已替换标记,防止重复运行 if (window._navLinksReplaced) { return; } // 已经替换过的链接集合,防止重复替换 const replacedLinks = new Set(); // 只在导航栏区域查找和替换链接 const headerArea = document.querySelector('header') || document.querySelector('nav'); if (!headerArea) { return; } // 在导航区域内查找链接 const navLinks = headerArea.querySelectorAll('a'); navLinks.forEach(link => { // 如果已经替换过,跳过 if (replacedLinks.has(link)) return; const linkText = link.textContent.trim(); const linkHref = link.getAttribute('href') || ''; // 替换Spaces链接 - 仅替换一次 if ( (linkHref.includes('/spaces') || linkHref === '/spaces' || linkText === 'Spaces' || linkText.match(/^s*Spacess*$/i)) && linkText !== 'OCR模型免费转Markdown' && linkText !== 'OCR模型免费转Markdown' ) { link.textContent = 'OCR模型免费转Markdown'; link.href = 'https://fast360.xyz'; link.setAttribute('target', '_blank'); link.setAttribute('rel', 'noopener noreferrer'); replacedLinks.add(link); } // 删除Posts链接 else if ( (linkHref.includes('/posts') || linkHref === '/posts' || linkText === 'Posts' || linkText.match(/^s*Postss*$/i)) ) { if (link.parentNode) { link.parentNode.removeChild(link); } replacedLinks.add(link); } // 替换Docs链接 - 仅替换一次 else if ( (linkHref.includes('/docs') || linkHref === '/docs' || linkText === 'Docs' || linkText.match(/^s*Docss*$/i)) && linkText !== '模型下载攻略' ) { link.textContent = '模型下载攻略'; link.href = '/'; replacedLinks.add(link); } // 删除Enterprise链接 else if ( (linkHref.includes('/enterprise') || linkHref === '/enterprise' || linkText === 'Enterprise' || linkText.match(/^s*Enterprises*$/i)) ) { if (link.parentNode) { link.parentNode.removeChild(link); } replacedLinks.add(link); } }); // 查找可能嵌套的Spaces和Posts文本 const textNodes = []; function findTextNodes(element) { if (element.nodeType === Node.TEXT_NODE) { const text = element.textContent.trim(); if (text === 'Spaces' || text === 'Posts' || text === 'Enterprise') { textNodes.push(element); } } else { for (const child of element.childNodes) { findTextNodes(child); } } } // 只在导航区域内查找文本节点 findTextNodes(headerArea); // 替换找到的文本节点 textNodes.forEach(node => { const text = node.textContent.trim(); if (text === 'Spaces') { node.textContent = node.textContent.replace(/Spaces/g, 'OCR模型免费转Markdown'); } else if (text === 'Posts') { // 删除Posts文本节点 if (node.parentNode) { node.parentNode.removeChild(node); } } else if (text === 'Enterprise') { // 删除Enterprise文本节点 if (node.parentNode) { node.parentNode.removeChild(node); } } }); // 标记已替换完成 window._navLinksReplaced = true; } // 替换代码区域中的域名 function replaceCodeDomains() { // 特别处理span.hljs-string和span.njs-string元素 document.querySelectorAll('span.hljs-string, span.njs-string, span[class*="hljs-string"], span[class*="njs-string"]').forEach(span => { if (span.textContent && span.textContent.includes('huggingface.co')) { span.textContent = span.textContent.replace(/huggingface.co/g, 'aifasthub.com'); } }); // 替换hljs-string类的span中的域名(移除多余的转义符号) document.querySelectorAll('span.hljs-string, span[class*="hljs-string"]').forEach(span => { if (span.textContent && span.textContent.includes('huggingface.co')) { span.textContent = span.textContent.replace(/huggingface.co/g, 'aifasthub.com'); } }); // 替换pre和code标签中包含git clone命令的域名 document.querySelectorAll('pre, code').forEach(element => { if (element.textContent && element.textContent.includes('git clone')) { const text = element.innerHTML; if (text.includes('huggingface.co')) { element.innerHTML = text.replace(/huggingface.co/g, 'aifasthub.com'); } } }); // 处理特定的命令行示例 document.querySelectorAll('pre, code').forEach(element => { const text = element.innerHTML; if (text.includes('huggingface.co')) { // 针对git clone命令的专门处理 if (text.includes('git clone') || text.includes('GIT_LFS_SKIP_SMUDGE=1')) { element.innerHTML = text.replace(/huggingface.co/g, 'aifasthub.com'); } } }); // 特别处理模型下载页面上的代码片段 document.querySelectorAll('.flex.border-t, .svelte_hydrator, .inline-block').forEach(container => { const content = container.innerHTML; if (content && content.includes('huggingface.co')) { container.innerHTML = content.replace(/huggingface.co/g, 'aifasthub.com'); } }); // 特别处理模型仓库克隆对话框中的代码片段 try { // 查找包含"Clone this model repository"标题的对话框 const cloneDialog = document.querySelector('.svelte_hydration_boundary, [data-target="MainHeader"]'); if (cloneDialog) { // 查找对话框中所有的代码片段和命令示例 const codeElements = cloneDialog.querySelectorAll('pre, code, span'); codeElements.forEach(element => { if (element.textContent && element.textContent.includes('huggingface.co')) { if (element.innerHTML.includes('huggingface.co')) { element.innerHTML = element.innerHTML.replace(/huggingface.co/g, 'aifasthub.com'); } else { element.textContent = element.textContent.replace(/huggingface.co/g, 'aifasthub.com'); } } }); } // 更精确地定位克隆命令中的域名 document.querySelectorAll('[data-target]').forEach(container => { const codeBlocks = container.querySelectorAll('pre, code, span.hljs-string'); codeBlocks.forEach(block => { if (block.textContent && block.textContent.includes('huggingface.co')) { if (block.innerHTML.includes('huggingface.co')) { block.innerHTML = block.innerHTML.replace(/huggingface.co/g, 'aifasthub.com'); } else { block.textContent = block.textContent.replace(/huggingface.co/g, 'aifasthub.com'); } } }); }); } catch (e) { // 错误处理但不打印日志 } } // 当DOM加载完成后执行替换 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', () => { replaceHeaderBranding(); replaceNavigationLinks(); replaceCodeDomains(); // 只在必要时执行替换 - 3秒后再次检查 setTimeout(() => { if (!window._navLinksReplaced) { console.log('[Client] 3秒后重新检查导航链接'); replaceNavigationLinks(); } }, 3000); }); } else { replaceHeaderBranding(); replaceNavigationLinks(); replaceCodeDomains(); // 只在必要时执行替换 - 3秒后再次检查 setTimeout(() => { if (!window._navLinksReplaced) { console.log('[Client] 3秒后重新检查导航链接'); replaceNavigationLinks(); } }, 3000); } // 增加一个MutationObserver来处理可能的动态元素加载 const observer = new MutationObserver(mutations => { // 检查是否导航区域有变化 const hasNavChanges = mutations.some(mutation => { // 检查是否存在header或nav元素变化 return Array.from(mutation.addedNodes).some(node => { if (node.nodeType === Node.ELEMENT_NODE) { // 检查是否是导航元素或其子元素 if (node.tagName === 'HEADER' || node.tagName === 'NAV' || node.querySelector('header, nav')) { return true; } // 检查是否在导航元素内部 let parent = node.parentElement; while (parent) { if (parent.tagName === 'HEADER' || parent.tagName === 'NAV') { return true; } parent = parent.parentElement; } } return false; }); }); // 只在导航区域有变化时执行替换 if (hasNavChanges) { // 重置替换状态,允许再次替换 window._navLinksReplaced = false; replaceHeaderBranding(); replaceNavigationLinks(); } }); // 开始观察document.body的变化,包括子节点 if (document.body) { observer.observe(document.body, { childList: true, subtree: true }); } else { document.addEventListener('DOMContentLoaded', () => { observer.observe(document.body, { childList: true, subtree: true }); }); } })(); \n \n )\n }\n\n4. Next.js already includes by default the [meta charset](https://developer.mozilla.org/docs/Web/HTML/Element/meta#charset) and [meta viewport](https://developer.mozilla.org/docs/Web/HTML/Viewport_meta_tag) tags, so you can safely remove those from your ``:\n\napp/layout.tsx\n\nTypeScript\n\nJavaScriptTypeScript\n\n export default function RootLayout({\n children,\n }: {\n children: React.ReactNode\n }) {\n return (\n \n \n \n My App\n \n \n \n
{children}
\n \n \n )\n }\n\n5. Any [metadata files](/docs/app/building-your-application/optimizing/metadata#file-based-metadata) such as `favicon.ico`, `icon.png`, `robots.txt` are automatically added to the application `` tag as long as you have them placed into the top level of the `app` directory. After moving [all supported files](/docs/app/building-your-application/optimizing/metadata#file-based-metadata) into the `app` directory you can safely delete their `` tags:\n\napp/layout.tsx\n\nTypeScript\n\nJavaScriptTypeScript\n\n export default function RootLayout({\n children,\n }: {\n children: React.ReactNode\n }) {\n return (\n \n \n My App\n \n \n \n
{children}
\n \n \n )\n }\n\n6. Finally, Next.js can manage your last `` tags with the [Metadata API](/docs/app/building-your-application/optimizing/metadata). Move your final metadata info into an exported [`metadata` object](/docs/app/api-reference/functions/generate-metadata#metadata-object):\n\napp/layout.tsx\n\nTypeScript\n\nJavaScriptTypeScript\n\n import type { Metadata } from 'next'\n \n export const metadata: Metadata = {\n title: 'My App',\n description: 'My App is a...',\n }\n \n export default function RootLayout({\n children,\n }: {\n children: React.ReactNode\n }) {\n return (\n \n \n
{children}
\n \n \n )\n }\n\nWith the above changes, you shifted from declaring everything in your `index.html` to using Next.js' convention-based approach built into the framework ([Metadata API](/docs/app/building-your-application/optimizing/metadata)). This approach enables you to more easily improve your SEO and web shareability of your pages.\n\n### [Step 5: Create the Entrypoint Page](#step-5-create-the-entrypoint-page)\n\nOn Next.js you declare an entrypoint for your application by creating a `page.tsx` file. The closest equivalent of this file on Vite is your `main.tsx` file. In this step, you’ll set up the entrypoint of your application.\n\n1. **Create a `[[...slug]]` directory in your `app` directory.**\n\nSince in this guide we're aiming first to set up our Next.js as an SPA (Single Page Application), you need your page entrypoint to catch all possible routes of your application. For that, create a new `[[...slug]]` directory in your `app` directory.\n\nThis directory is what is called an [optional catch-all route segment](/docs/app/building-your-application/routing/dynamic-routes#optional-catch-all-segments). Next.js uses a file-system based router where [directories are used to define routes](/docs/app/building-your-application/routing/defining-routes#creating-routes). This special directory will make sure that all routes of your application will be directed to its containing `page.tsx` file.\n\n2. **Create a new `page.tsx` file inside the `app/[[...slug]]` directory with the following content:**\n\napp/\\[\\[...slug\\]\\]/page.tsx\n\nTypeScript\n\nJavaScriptTypeScript\n\n import '../../index.css'\n \n export function generateStaticParams() {\n return [{ slug: [''] }]\n }\n \n export default function Page() {\n return '...' // We'll update this\n }\n\n> **Good to know**: `.js`, `.jsx`, or `.tsx` extensions can be used for Page files.\n\nThis file is a [Server Component](/docs/app/building-your-application/rendering/server-components). When you run `next build`, the file is prerendered into a static asset. It does _not_ require any dynamic code.\n\nThis file imports our global CSS and tells [`generateStaticParams`](/docs/app/api-reference/functions/generate-static-params) we are only going to generate one route, the index route at `/`.\n\nNow, let's move the rest of our Vite application which will run client-only.\n\napp/\\[\\[...slug\\]\\]/client.tsx\n\nTypeScript\n\nJavaScriptTypeScript\n\n 'use client'\n \n import React from 'react'\n import dynamic from 'next/dynamic'\n \n const App = dynamic(() => import('../../App'), { ssr: false })\n \n export function ClientOnly() {\n return \n }\n\nThis file is a [Client Component](/docs/app/building-your-application/rendering/client-components), defined by the `'use client'` directive. Client Components are still [prerendered to HTML](/docs/app/building-your-application/rendering/client-components#how-are-client-components-rendered) on the server before being sent to the client.\n\nSince we want a client-only application to start, we can configure Next.js to disable prerendering from the `App` component down.\n\n const App = dynamic(() => import('../../App'), { ssr: false })\n\nNow, update your entrypoint page to use the new component:\n\napp/\\[\\[...slug\\]\\]/page.tsx\n\nTypeScript\n\nJavaScriptTypeScript\n\n import '../../index.css'\n import { ClientOnly } from './client'\n \n export function generateStaticParams() {\n return [{ slug: [''] }]\n }\n \n export default function Page() {\n return \n }\n\n### [Step 6: Update Static Image Imports](#step-6-update-static-image-imports)\n\nNext.js handles static image imports slightly different from Vite. With Vite, importing an image file will return its public URL as a string:\n\nApp.tsx\n\n import image from './img.png' // `image` will be '/assets/img.2d8efhg.png' in production\n \n export default function App() {\n return \n }\n\nWith Next.js, static image imports return an object. The object can then be used directly with the Next.js [`` component](/docs/app/api-reference/components/image), or you can use the object's `src` property with your existing `` tag.\n\nThe `` component has the added benefits of [automatic image optimization](/docs/app/building-your-application/optimizing/images). The `` component automatically sets the `width` and `height` attributes of the resulting `` based on the image's dimensions. This prevents layout shifts when the image loads. However, this can cause issues if your app contains images with only one of their dimensions being styled without the other styled to `auto`. When not styled to `auto`, the dimension will default to the `` dimension attribute's value, which can cause the image to appear distorted.\n\nKeeping the `` tag will reduce the amount of changes in your application and prevent the above issues. You can then optionally later migrate to the `` component to take advantage of optimizing images by [configuring a loader](/docs/app/building-your-application/optimizing/images#loaders), or moving to the default Next.js server which has automatic image optimization.\n\n1. **Convert absolute import paths for images imported from `/public` into relative imports:**\n\n // Before\n import logo from '/logo.png'\n \n // After\n import logo from '../public/logo.png'\n\n2. **Pass the image `src` property instead of the whole image object to your `` tag:**\n\n // Before\n \n \n // After\n \n\nAlternatively, you can reference the public URL for the image asset based on the filename. For example, `public/logo.png` will serve the image at `/logo.png` for your application, which would be the `src` value.\n\n> **Warning:** If you're using TypeScript, you might encounter type errors when accessing the `src` property. You can safely ignore those for now. They will be fixed by the end of this guide.\n\n### [Step 7: Migrate the Environment Variables](#step-7-migrate-the-environment-variables)\n\nNext.js has support for `.env` [environment variables](/docs/app/building-your-application/configuring/environment-variables) similar to Vite. The main difference is the prefix used to expose environment variables on the client-side.\n\n* Change all environment variables with the `VITE_` prefix to `NEXT_PUBLIC_`.\n\nVite exposes a few built-in environment variables on the special `import.meta.env` object which aren’t supported by Next.js. You need to update their usage as follows:\n\n* `import.meta.env.MODE` ⇒ `process.env.NODE_ENV`\n* `import.meta.env.PROD` ⇒ `process.env.NODE_ENV === 'production'`\n* `import.meta.env.DEV` ⇒ `process.env.NODE_ENV !== 'production'`\n* `import.meta.env.SSR` ⇒ `typeof window !== 'undefined'`\n\nNext.js also doesn't provide a built-in `BASE_URL` environment variable. However, you can still configure one, if you need it:\n\n1. **Add the following to your `.env` file:**\n\n.env\n\n # ...\n NEXT_PUBLIC_BASE_PATH=\"/some-base-path\"\n\n2. **Set [`basePath`](/docs/app/api-reference/next-config-js/basePath) to `process.env.NEXT_PUBLIC_BASE_PATH` in your `next.config.mjs` file:**\n\nnext.config.mjs\n\n /** @type {import('next').NextConfig} */\n const nextConfig = {\n output: 'export', // Outputs a Single-Page Application (SPA).\n distDir: './dist', // Changes the build output directory to `./dist/`.\n basePath: process.env.NEXT_PUBLIC_BASE_PATH, // Sets the base path to `/some-base-path`.\n }\n \n export default nextConfig\n\n3. **Update `import.meta.env.BASE_URL` usages to `process.env.NEXT_PUBLIC_BASE_PATH`**\n\n### [Step 8: Update Scripts in `package.json`](#step-8-update-scripts-in-packagejson)\n\nYou should now be able to run your application to test if you successfully migrated to Next.js. But before that, you need to update your `scripts` in your `package.json` with Next.js related commands, and add `.next` and `next-env.d.ts` to your `.gitignore`:\n\npackage.json\n\n {\n \"scripts\": {\n \"dev\": \"next dev\",\n \"build\": \"next build\",\n \"start\": \"next start\"\n }\n }\n\n.gitignore\n\n # ...\n .next\n next-env.d.ts\n dist\n\nNow run `npm run dev`, and open [`http://localhost:3000`](http://localhost:3000). You should see your application now running on Next.js.\n\n> **Example:** Check out [this pull request](https://github.com/inngest/vite-to-nextjs/pull/1) for a working example of a Vite application migrated to Next.js.\n\n### [Step 9: Clean Up](#step-9-clean-up)\n\nYou can now clean up your codebase from Vite related artifacts:\n\n* Delete `main.tsx`\n* Delete `index.html`\n* Delete `vite-env.d.ts`\n* Delete `tsconfig.node.json`\n* Delete `vite.config.ts`\n* Uninstall Vite dependencies\n\n[Next Steps](#next-steps)\n-------------------------\n\nIf everything went according to plan, you now have a functioning Next.js application running as a single-page application. However, you aren't yet taking advantage of most of Next.js' benefits, but you can now start making incremental changes to reap all the benefits. Here's what you might want to do next:\n\n* Migrate from React Router to the [Next.js App Router](/docs/app/building-your-application/routing) to get:\n * Automatic code splitting\n * [Streaming Server-Rendering](/docs/app/building-your-application/routing/loading-ui-and-streaming)\n * [React Server Components](/docs/app/building-your-application/rendering/server-components)\n* [Optimize images with the `` component](/docs/app/building-your-application/optimizing/images)\n* [Optimize fonts with `next/font`](/docs/app/building-your-application/optimizing/fonts)\n* [Optimize third-party scripts with the `