打开终端,执行以下命令安装Node.js(版本需≥16.0):
``` curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash source ~/.bashrc nvm install 18 nvm use 18 ```验证安装是否成功:
``` node -v npm -v ```创建项目目录并初始化:
``` mkdir official-website && cd official-website npm init -y ```安装核心依赖:
``` npm install next@13 react@18 react-dom@18 npm install -D tailwindcss@3 postcss autoprefixer ```初始化Tailwind CSS配置文件:
``` npx tailwindcss init -p ```在项目根目录创建以下目录结构:
``` official-website/ ├── app/ │ ├── layout.js │ ├── page.js │ ├── about/ │ │ └── page.js │ └── contact/ │ └── page.js ├── components/ ├── public/ │ ├── images/ │ └── favicon.ico └── tailwind.config.js ```编辑app/layout.js文件:
``` import './globals.css' export const metadata = { title: '企业官方网站', description: '专业的企业官网解决方案', } export default function RootLayout({ children }) { return (编辑app/page.js文件:
``` import HeroSection from '@/components/HeroSection' import Features from '@/components/Features' export default function Home() { return ( <>创建components/HeroSection.js:
``` export default function HeroSection() { return (我们提供专业的技术解决方案,助力企业数字化转型
在项目根目录创建.env.local文件:
``` NEXT_PUBLIC_API_URL=https://api.example.com NEXT_PUBLIC_SITE_URL=https://www.yourdomain.com NEXT_PUBLIC_GA_MEASUREMENT_ID=G-XXXXXXXXXX ```
创建app/metadata.js文件:
``` export const generateMetadata = ({ params }) => { return { title: '企业官方网站 - 专业解决方案提供商', description: '我们提供全面的技术服务和产品解决方案', keywords: '企业官网,技术方案,数字化转型', openGraph: { title: '企业官方网站', description: '专业的企业官网解决方案', images: ['/og-image.png'], }, } } ```创建app/sitemap.js文件:
``` export default function sitemap() { const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || 'https://www.yourdomain.com' return [ { url: baseUrl, lastModified: new Date(), changeFrequency: 'weekly', priority: 1, }, { url: `${baseUrl}/about`, lastModified: new Date(), changeFrequency: 'monthly', priority: 0.8, }, { url: `${baseUrl}/contact`, lastModified: new Date(), changeFrequency: 'monthly', priority: 0.8, }, ] } ```编辑next.config.js文件:
``` / @type {import('next').NextConfig} / const nextConfig = { reactStrictMode: true, images: { domains: ['assets.example.com'], }, compress: true, } module.exports = nextConfig ```编辑tailwind.config.js文件:
``` / @type {import('tailwindcss').Config} / module.exports = { content: [ './app//.{js,ts,jsx,tsx,mdx}', './components//.{js,ts,jsx,tsx,mdx}', ], theme: { extend: { container: { center: true, padding: '1rem', }, }, }, plugins: [], } ```创建vercel.json配置文件:
``` { "buildCommand": "npm run build", "devCommand": "npm run dev", "installCommand": "npm install", "framework": "nextjs", "outputDirectory": ".next", "regions": ["hkg1"], "env": { "NODE_ENV": "production" } } ```执行部署命令:
``` npm run build npx vercel --prod ```在Vercel控制台完成部署后:
安装Sentry进行错误追踪:
``` npm install @sentry/nextjs ```执行Sentry初始化:
``` npx @sentry/wizard@latest -i nextjs ```按照向导提示完成配置,生成sentry.client.config.js和sentry.server.config.js文件。
创建.github/workflows/deploy.yml文件:
``` name: Deploy to Vercel on: push: branches: [main] jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: amondnet/vercel-action@v20 with: vercel-token: ${{ secrets.VERCEL_TOKEN }} vercel-org-id: ${{ secrets.ORG_ID}} vercel-project-id: ${{ secrets.PROJECT_ID}} ```在GitHub仓库设置中配置对应的secrets:VERCEL_TOKEN、ORG_ID、PROJECT_ID。
完成以上所有步骤后,你的企业级官方网站将具备完整的前端功能、SEO优化、性能监控和自动化部署能力,可以直接投入生产使用。
易频IT社区是综合性互联网IT技术门户网站,专注分享网络技术、服务器运维、网络安全、编程开发、系统架构、云计算、大数据等行业干货,实时更新IT行业资讯、零基础教程、实战案例,为IT从业者、技术爱好者提供专业的学习交流平台。
Copyright © 2021-2026 易频IT社区. All Rights Reserved. 备案号:闽ICP备2023013482号 网站地图