Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 0 additions & 201 deletions apps/www.giuem.com/vercel.json

This file was deleted.

72 changes: 63 additions & 9 deletions packages/gatsby-plugin-vercel-json/gatsby-node.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,66 @@
import fs from 'fs';
import { resolve } from 'path';

import type { GatsbyNode, Actions } from 'gatsby';
import type { GatsbyNode, Actions, Page } from 'gatsby';

type VercelRedirect = {
source: string;
destination: string;
permanent?: boolean;
};

type VercelHeaders = {
source: string;
headers: {
key: string;
value: string;
}[];
};

type GatsbyRedirect = Parameters<Actions['createRedirect']>[0];

type GatsbyState = {
redirects: GatsbyRedirect[];
// pages: Map<string, Page>;
// program: { directory: string };
pages: Map<string, Page>;
program: { directory: string };
};

type PluginOptions = {
cleanUrls?: boolean;
headers: VercelHeaders[];
};

export const onPostBuild: GatsbyNode['onPostBuild'] = async ({ store }) => {
const { redirects }: GatsbyState = store.getState();
export const pluginOptionsSchema: GatsbyNode['pluginOptionsSchema'] = ({
Joi,
}) =>
Joi.object<PluginOptions>({
cleanUrls: Joi.boolean().default(false),
headers: Joi.array().items(
Joi.object({
source: Joi.string().required(),
headers: Joi.array()
.items(
Joi.object({
key: Joi.string().required(),
value: Joi.string().required(),
})
)
.required(),
})
),
});

export const onPostBuild: GatsbyNode['onPostBuild'] = async (
{ store, reporter },
pluginOptions
) => {
console.log(pluginOptions);
const { cleanUrls, headers } = pluginOptions as unknown as PluginOptions;
const { redirects, program }: GatsbyState = store.getState();

const publicDir = resolve(program.directory, 'public');
const vercelJsonPath = resolve(publicDir, 'vercel.json');
throw 1;

const vercelRedirects: VercelRedirect[] = [];
redirects.forEach((redirect) => {
Expand All @@ -29,11 +71,23 @@ export const onPostBuild: GatsbyNode['onPostBuild'] = async ({ store }) => {
});
});

if (redirects.length > 1024) {
reporter.panicOnBuild(
`Vercel supports redirects up to 1024, but got ${redirects.length}`
);
}

// eslint-disable-next-line @typescript-eslint/no-var-requires
const vercelJson = require('./vercel.json');
vercelJson.redirects = vercelRedirects;
await fs.promises.writeFile(
resolve('vercel.json'),
const vercelJson = {
cleanUrls,
headers,
// @todo get from config
trailingSlash: false,
redirects: vercelRedirects,
};

return fs.promises.writeFile(
vercelJsonPath,
JSON.stringify(vercelJson, null, 2),
'utf-8'
);
Expand Down