diff --git a/.github/workflows/publish-gh-pages-hack.yml b/.github/workflows/publish-gh-pages-hack.yml index b333b9020..432268bb8 100644 --- a/.github/workflows/publish-gh-pages-hack.yml +++ b/.github/workflows/publish-gh-pages-hack.yml @@ -30,39 +30,18 @@ jobs: - name: Install deps run: yarn install --frozen-lockfile - - name: Build next.js (hacky static export) - run: | - export VERCEL_GIT_COMMIT_SHA="$(git rev-parse HEAD)" - export VERCEL_GIT_COMMIT_MESSAGE="$(git log -1 --pretty=%B)" - export NEXT_PUBLIC_FAKE_STATIC_EXPORT=true - export NEXT_PUBLIC_FORCE_PROJECT=osmapp.org - export NEXT_PUBLIC_OG_IMAGE_FETCHER_HOST=https://osmapp-preview.vercel.app - yarn build - mkdir public/_next - cp -r .next/static public/_next - #echo -e "User-agent: *\nDisallow: /" > public/robots.txt - - yarn start & - sleep 5 # server usually starts in 1 second - - curl --cookie "hideHomepage=yes" http://localhost:3000 > public/index.html - curl --cookie "hideHomepage=yes" http://localhost:3000 > public/404.html - for lang in $(node --input-type=module <<<'import { LANGUAGES } from "./src/config.mjs";process.stdout.write(Object.keys(LANGUAGES).join("\n")+"\n");'); do - mkdir public/$lang - curl --cookie "hideHomepage=yes; lang=$lang" http://localhost:3000 > public/$lang/index.html - done; - - # cache SSR version of some pages (eg. Prague, english-only) - mkdir -p public/node/1601837931 - curl --cookie "hideHomepage=yes" http://localhost:3000/node/1601837931 > public/node/1601837931/index.html + - name: Run hacky-static-export + run: yarn hacky-static-export - name: Setup Pages id: pages uses: actions/configure-pages@v5 + - name: Upload artifact uses: actions/upload-pages-artifact@v4 with: - path: ./public + path: ./static-export + - name: Deploy to GitHub Pages id: deployment uses: actions/deploy-pages@v4 diff --git a/.gitignore b/.gitignore index ed84fc933..98640524c 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ yarn-error.log* .env.local .env.sentry-build-plugin /public/icons-indoor +static-export diff --git a/package.json b/package.json index 68420516e..b7d38bf7b 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "build": "next build", "start": "next start", "dist": "next build && next export -o dist", + "hacky-static-export": "bash scripts/hacky-static-export.sh", "postinstall": "copyfiles -u 2 node_modules/maplibre-gl-indoorequal/sprite/* public/icons-indoor/" }, "husky": { diff --git a/scripts/hacky-static-export.sh b/scripts/hacky-static-export.sh new file mode 100755 index 000000000..710ec9d48 --- /dev/null +++ b/scripts/hacky-static-export.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +set -euom pipefail + +if lsof -iTCP:3000 -sTCP:LISTEN -t > /dev/null 2>&1; then + echo "Error: port 3000 is already in use." >&2 + exit 1 +fi + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" +OUT="$PROJECT_ROOT/static-export" + +cd "$PROJECT_ROOT" + +export VERCEL_GIT_COMMIT_SHA="$(git rev-parse HEAD)" +export VERCEL_GIT_COMMIT_MESSAGE="$(git log -1 --pretty=%B)" +export NEXT_PUBLIC_FAKE_STATIC_EXPORT=true +export NEXT_PUBLIC_FORCE_PROJECT=osmapp.org +export NEXT_PUBLIC_OG_IMAGE_FETCHER_HOST=https://osmapp-preview.vercel.app +export NEXT_PUBLIC_API_KEY_MAPTILER=yd6CSMQpAwnAcPVZwWQ6 + +yarn build + +rm -rf "$OUT" +cp -r public "$OUT" +mkdir -p "$OUT/_next" +cp -r .next/static "$OUT/_next" + +yarn start > /dev/null 2>&1 & +SERVER_PID=$! +cleanup() { kill -- -"$SERVER_PID" 2>/dev/null || true; } # works thanks to `set -m` +trap cleanup EXIT + +for i in $(seq 1 5); do curl -sf localhost:3000/node/6 > /dev/null 2>&1 && break; sleep 5; done + +echo "> Checking SSR..." +SSR_HTML="$(curl --silent --fail localhost:3000/node/6 || true)" +if grep -q "Originally DetonĂ¡tor route (this message used for SSR check)" <<< "$SSR_HTML"; then + echo "SSR OK" +else + echo "SSR FAILED"; exit 1 +fi + +echo "> Get index.html and 404.html..." +curl -s --cookie "hideHomepage=yes" localhost:3000 > "$OUT/index.html" +curl -s --cookie "hideHomepage=yes" localhost:3000 > "$OUT/404.html" + +for lang in $(node --input-type=module <<<'import { LANGUAGES } from "./src/config.mjs"; process.stdout.write(Object.keys(LANGUAGES).join("\n")+"\n");'); do + echo "> Get language $lang..." + mkdir -p "$OUT/$lang" + curl -s --cookie "hideHomepage=yes; lang=$lang" localhost:3000 > "$OUT/$lang/index.html" +done + +echo "> Get Prague node page..." +mkdir -p "$OUT/node/1601837931" +curl -s --cookie "hideHomepage=yes" localhost:3000/node/1601837931 > "$OUT/node/1601837931/index.html" + +echo "" +echo "Output in: $OUT"