128 lines
3.9 KiB
TypeScript
128 lines
3.9 KiB
TypeScript
// nitro hook to get Directus files working on ssg
|
|
// https://github.com/codepie-io/nuxt3-dynamic-routes/blob/main/nuxt.config.ts
|
|
// + ssg homemade caching to not retrieve all the files each generation
|
|
|
|
import { createDirectus, staticToken, rest, readFiles } from '@directus/sdk';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import { promisify } from 'util';
|
|
|
|
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) {
|
|
|
|
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) {
|
|
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));
|
|
}
|
|
},
|
|
},
|
|
prerender: {
|
|
routes: [
|
|
'/api/items/global'
|
|
]
|
|
},
|
|
},
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
},
|
|
app: {
|
|
pageTransition: { name: 'page', mode: 'out-in' }
|
|
},
|
|
site: {
|
|
url: process.env.URL,
|
|
defaultLocale: 'fr',
|
|
name: 'Mahée Auffret',
|
|
description: 'Portfolio de l\'artiste-peintre basée à Rennes Mahée Auffret'
|
|
}
|
|
})
|