mahee-auffret/nuxt.config.ts

123 lines
3.8 KiB
TypeScript
Raw Normal View History

// nitro hook to get Directus files working on ssg
// https://github.com/codepie-io/nuxt3-dynamic-routes/blob/main/nuxt.config.ts
2024-04-12 00:23:57 +02:00
// + ssg homemade caching to not retrieve all the files each generation
import { createDirectus, staticToken, rest, readFiles } from '@directus/sdk';
2024-04-12 00:23:57 +02:00
import fs from 'fs';
import path from 'path';
import { promisify } from 'util';
2024-02-21 00:30:14 +01:00
export default defineNuxtConfig({
devtools: { enabled: true },
modules: [
'@nuxtjs/seo'
],
runtimeConfig: {
apiURL: process.env.DIRECTUS_URL,
apiToken: process.env.DIRECTUS_API_TOKEN
},
nitro: {
hooks: {
async 'prerender:routes'(routes) {
2024-04-12 00:23:57 +02:00
const client = createDirectus(process.env.DIRECTUS_URL)
.with(staticToken(process.env.DIRECTUS_API_TOKEN))
.with(rest());
const directusFiles = await client.request(
readFiles({
query: {
filter: {
type: {
_eq: 'image',
},
},
},
})
);
for (let image of directusFiles) {
2024-04-12 00:23:57 +02:00
const fileExists = async (filePath) => !!(await fs.promises.access(filePath, fs.constants.F_OK).then(() => true).catch(() => false));
const filePath = `./public/api/assets/${image.id}.webp`;
fileExists(filePath)
.then(exists => {
if (!exists) {
routes.add(`/api/assets/${image.id}.webp`);
}
})
.catch(error => console.error('Error:', error));
}
},
},
},
hooks: {
'nitro:build:public-assets': () => {
if (!fs.existsSync('./public/api/assets')) fs.mkdirSync('./public/api/assets', { recursive: true });
const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);
const copyFile = promisify(fs.copyFile);
async function directoryExists(directoryPath) {
try {
const stats = await fs.promises.stat(directoryPath);
return stats.isDirectory();
} catch (error) {
if (error.code === 'ENOENT') {
return false;
} else {
throw error;
}
}
}
2024-04-12 00:23:57 +02:00
async function copyFilesIfNotExist(sourceFolder, destinationFolder) {
try {
const exists = await directoryExists(sourceFolder);
if (!exists) {
console.log(`Source folder '${sourceFolder}' does not exist.`);
return;
}
const files = await readdir(sourceFolder);
for (const file of files) {
const sourceFilePath = path.join(sourceFolder, file);
const destinationFilePath = path.join(destinationFolder, file);
const sourceFileStat = await stat(sourceFilePath);
if (sourceFileStat.isFile()) {
try {
await stat(destinationFilePath);
} catch (error) {
if (error.code === 'ENOENT') {
await copyFile(sourceFilePath, destinationFilePath);
console.log(`Copied '${file}' to '${destinationFolder}'.`);
} else {
throw error;
}
}
}
}
console.log('Files copied successfully.');
} catch (error) {
console.error('Error:', error);
}
}
const sourceFolder = './.output/public/api/assets';
const destinationFolder = './public/api/assets';
copyFilesIfNotExist(sourceFolder, destinationFolder);
}
},
2024-02-21 00:30:14 +01:00
app: {
pageTransition: { name: 'page', mode: 'out-in' }
},
site: {
url: process.env.URL,
2024-02-21 00:30:14 +01:00
defaultLocale: 'fr',
name: 'Mahée Auffret',
description: 'Portfolio de l\'artiste-peintre basée à Rennes Mahée Auffret'
}
})