-
-
Notifications
You must be signed in to change notification settings - Fork 52
@astrojs/sitemap excluded from final static output with Vercel adapter #445
Comments
I have the same issue. Sitemap does exist when I build locally but is missing when I explore build output on Vercel deployment. "@astrojs/sitemap": "^3.2.1",
"@astrojs/vercel": "8.0.0",
"astro": "^5.0.3", |
Workaround I used: Use a hook for after the build process: 'astro:build:done': async ({ logger }) => {
const buildLogger = logger.fork('sitemap-copier');
buildLogger.info("Copying xml files from dist to vercel out");
try {
const files = await fs.readdir('./dist/client');
const xmlFiles = files.filter(file =>
path.extname(file).toLowerCase() === '.xml' &&
path.basename(file).toLowerCase().startsWith('sitemap')
);
buildLogger.info(xmlFiles)
for (const file of xmlFiles) {
const sourcePath = path.join('./dist/client', file);
const destPath = path.join('./.vercel/output/static', file);
await fs.copyFile(sourcePath, destPath);
}
buildLogger.info('All XML files copied successfully');
} catch (error) {
buildLogger.error('Error copying files:', error);
}
} |
Here is a full integration based on @ichbinstudent code.
import type { AstroIntegration } from "astro";
import { readdir, cp } from "node:fs/promises";
import * as path from "node:path";
export function sitemapCopier(): AstroIntegration {
return {
name: "sitemap-copier",
hooks: {
"astro:build:done": async ({ logger }) => {
const buildLogger = logger.fork("sitemap-copier");
buildLogger.info("Copying xml files from dist to vercel out");
try {
const files = await readdir("./dist/client");
const xmlFiles = files.filter(
(file) =>
path.extname(file).toLowerCase() === ".xml" &&
path.basename(file).toLowerCase().startsWith("sitemap")
);
buildLogger.info(xmlFiles.join(", "));
for (const file of xmlFiles) {
const sourcePath = path.join("./dist/client", file);
const destPath = path.join("./.vercel/output/static", file);
await cp(sourcePath, destPath);
}
buildLogger.info("All XML files copied successfully");
} catch (error) {
buildLogger.error(`Error copying files: ${error}`);
}
}
}
};
}
import { sitemapCopier } from "./sitemap-copier.ts";
export default defineConfig({
//...
integrations: [
sitemap({
// your sitemap config
}),
sitemapCopier()
]
}); |
I'm also having this issue after upgrading to astro v5.0.4. Using the copy workaround for now. |
Same issue here after upgrading to astro v5 |
Same problem occurs with starlight on astro 5. I was able to use @phenomen's fix (thank you!) but had to adjust the path ./dist/client to ./dist |
This is an issue of the Vercel adapter, in combination with the sitemap integration. We haven't found a good fix yet, so moving manually the sitemap file is the best workaround |
Have the same issue. And not only with So it seems it isn't problem of Vercel adapter particularly, but of execution order of integrations. |
Hope this gets fixed soon 🙏 |
Well, maybe |
I’m encountering a similar issue where I’m using some Astro compression plugins to compress JavaScript and remove comments from the code. During the build process, I noticed that the files inside It seems like the Vercel build process runs before other integrations take effect, causing this discrepancy. Thanks to the suggestions from @ichbinstudent and @phenomen , I’ve created a small script that simplifies the process of copying files from the
import type {AstroIntegration} from 'astro';
import {promises as fs} from 'fs';
import path from 'path';
import {fileURLToPath} from 'url';
// Utility function for formatted logging
function formatLog(tag: string, message: string) {
const timestamp = new Date().toLocaleTimeString('en-US', {
hour: '2-digit',
hour12: false,
minute: '2-digit',
second: '2-digit'
});
// eslint-disable-next-line no-console
console.log(
'\n' + // Add space above
`\x1b[90m${timestamp}\x1b[0m ` + // Gray timestamp
`[\x1b[36m${tag}\x1b[0m] ` + // Cyan colored tag
`${message}` + // Message
'\n' // Add space below
);
}
async function copyFiles(srcDir: string, destDir: string) {
const files = await fs.readdir(srcDir);
for (const file of files) {
const srcPath = path.join(srcDir, file);
const destPath = path.join(destDir, file);
const stat = await fs.stat(srcPath);
if (stat.isDirectory()) {
await fs.mkdir(destPath, {recursive: true});
await copyFiles(srcPath, destPath);
} else {
await fs.copyFile(srcPath, destPath);
}
}
}
export function CopyFilesPlugin(): AstroIntegration {
return {
hooks: {
'astro:build:done': async ({dir}) => {
formatLog('copy-files', 'Copying files to .vercel/output/static');
const distDir = fileURLToPath(dir.href);
const staticDir = path.resolve('.vercel/output/static');
await fs.mkdir(staticDir, {recursive: true});
await copyFiles(distDir, staticDir);
}
},
name: 'copy-files'
};
}
import {CopyFilesPlugin} from './copy-files.ts';
export default defineConfig({
//...
integrations: [
// other integrations
CopyFilesPlugin()
]
}); |
But how do you make sure your integration runs the last one? By the declaration order? I think Astro should implement a specific configuration for adapters. Will fix this problem. |
Yes, it's determined by the declaration order. The script hook runs after Astro finishes building, so it’s best to place |
Thanks @mohdlatif you are amazing 🚀 |
…yment error - sitemaps not available - search (pagefind) is not working - gh issues: - withastro/adapters#445 - withastro/astro#12663 - arcjet/arcjet-docs#332
Thanks for the great workaround script! |
the fix in @astrojs/vercel 8.0.3 doesn't seem to work. sitemap files are still in dist/client instead of .vercel/output/static can we have this reopened please? EDIT: i reproduced it in the minimal repro. adding a non-static ( 13:44:44 [build] output: "server"
13:44:44 [build] directory: /home/nize/dev/playground/astro-sitemap-vercel-static/dist/
...
13:44:46 [build] Rearranging server assets...
13:44:46 [@astrojs/vercel] Bundling function ../../../../dist/server/entry.mjs
13:44:47 [@astrojs/sitemap] `sitemap-index.xml` created at `dist/client`
13:44:47 [build] Server built in 4.17s
13:44:47 [build] Complete! in fully static mode with no hybrid routes. the fix works as expected: 13:48:22 [build] output: "static"
13:48:22 [build] directory: /home/nize/dev/playground/astro-sitemap-vercel-static/dist/
...
13:48:24 [@astrojs/sitemap] `sitemap-index.xml` created at `dist`
13:48:24 [build] 8 page(s) built in 2.61s
13:48:24 [build] Complete! |
Issue isn't fixed for me either :/ |
Can you try upgrading to the latest version of |
#516 fixed it, thank you! |
I can still reproduce this unfortunately on https://skyfall.dev unfortunately :/ (repo: https://github.com/SkyfallWasTaken/skyfall-site) I do have another integration with similar behaviour ( |
Fixed for me as well, thank you! |
…yment error - sitemaps not available - search (pagefind) is not working - gh issues: - withastro/adapters#445 - withastro/astro#12663 - arcjet/arcjet-docs#332
Astro Info
If this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
output is
static
Using the versions above, the sitemap-index.xml and sitemap-0.xml files are created in
/dist
instead of.vercel/output/static
, excluding them from the final vercel deployment.What's the expected result?
astro-v4
branch in the reprooutput is
hybrid
No
/dist
folder is created at all after building, and the sitemap is properly generated in.vercel/output/static
.Link to Minimal Reproducible Example
https://github.com/dotnize/astro-sitemap-vercel-static
Participation
The text was updated successfully, but these errors were encountered: