Skip to content

Commit

Permalink
fix sitemap export missing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Moroshima committed Nov 9, 2023
1 parent 46af480 commit a6cecc2
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 10 deletions.
43 changes: 36 additions & 7 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,46 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: lts/*
- uses: pnpm/action-setup@v2

- name: Install pnpm package manager
uses: pnpm/action-setup@v2
with:
version: latest
- run: pnpm install
- run: pnpm build
- uses: cloudflare/[email protected]

- name: Cache pnpm package store directory
id: cache-pnpm-store
uses: actions/cache@v3
env:
cache-name: cache-pnpm-store-directory
with:
# the pnpm package store directory path is ` ~/.local/share/pnpm/store` on GNU/Linux
path: ~/.local/share/pnpm/store
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- if: ${{ steps.cache-pnpm-store.outputs.cache-hit != 'true' }}
name: List the state of node modules
continue-on-error: true
run: pnpm list

- name: Install dependencies
run: pnpm install

- name: Build
run: pnpm build

- name: Deploy to Cloudflare Pages
uses: cloudflare/[email protected]
with:
apiToken: ${{ secrets.CF_API_TOKEN }}
accountId: ${{ secrets.CF_ACCOUNT_ID }}
workingDirectory: 'out'
command: pages deploy --project-name=yunagi .
workingDirectory: "dist"
command: pages deploy --project-name=yunagi .
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

# next.js
/.next/
/out/
/dist/

# production
/build
Expand Down
6 changes: 5 additions & 1 deletion next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ import { visit } from "unist-util-visit";

/** @type {import('next').NextConfig} */
const nextConfig = {
output: "export",
// Configure pageExtensions to include md and mdx
pageExtensions: ["ts", "tsx", "js", "jsx", "md", "mdx"],
// Optionally, add any other Next.js config below
reactStrictMode: true,
output: "export",
// Prevent automatic `/me` -> `/me/`, instead preserve `href`
skipTrailingSlashRedirect: true,
// Change the output directory `out` -> `dist`
distDir: "dist",
};

// Merge MDX config with Next.js config
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"dev": "next dev",
"build": "next build",
"postbuild": "next-sitemap",
"postbuild": "next-sitemap && node ./sync-sitemap.js",
"start": "next start",
"lint": "next lint"
},
Expand Down
44 changes: 44 additions & 0 deletions sync-sitemap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const fs = require("fs/promises");
const path = require("path");

async function copyFiles() {
const publicDir = "public";
const distDir = "dist";
const filesToCopy = ["sitemap.xml", "robots.txt"];

console.log("Starting to copy files...");
console.log("Checking if dist directory exists...");

try {
// 使用 fs.promises.access 检查目录是否存在
await fs.access(distDir);

// 如果 access 没有抛出错误,说明目录存在,可以继续执行操作
console.log(`Directory ${distDir} exists.`);
console.log("Copying files...");

// 使用 Promise.all 来并行拷贝文件
await Promise.all(
filesToCopy.map(async (file) => {
const sourcePath = path.join(publicDir, file);
const destinationPath = path.join(distDir, file);
try {
await fs.copyFile(sourcePath, destinationPath);
console.log(`File ${file} copied successfully.`);
} catch (error) {
console.error(`Error copying ${file}: ${error.message}`);
}
})
);

console.log("All files copied successfully.");
} catch (error) {
// 如果目录不存在,输出提示信息
console.log(
`Directory ${distDir} does not exist. Please run "pnpm build" before running this script.`
);
process.exit(1);
}
}

copyFiles();

0 comments on commit a6cecc2

Please sign in to comment.