Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add COOP & COEP headers for SvelteKit, NextJS, & NuxtJS #279

Merged
merged 7 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
91 changes: 75 additions & 16 deletions src/lib/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,13 @@ async function project({ name, ui }) {
if (ui) {
switch (ui) {
case 'svelte':
// `-y` installs the latest version of create-svelte without prompting.
spawnSync('npm', ['create', 'svelte@latest', '-y', 'ui'], {
stdio: 'inherit',
shell: true,
});
scaffoldSvelte();
break;
case 'next':
// https://nextjs.org/docs/api-reference/create-next-app#options
spawnSync('npx', ['create-next-app@latest', 'ui', '--use-npm'], {
stdio: 'inherit',
shell: true,
});
sh.rm('-rf', path.join('ui', 'git')); // Remove NextJS' .git; we will init .git in our monorepo's root.
scaffoldNext();
break;
case 'nuxt':
console.log(" Choose 'no version control' when prompted.");
spawnSync('npx', ['create-nuxt-app', 'ui'], {
stdio: 'inherit',
shell: true,
});
scaffoldNuxt();
break;
case 'empty':
sh.mkdir('ui');
Expand Down Expand Up @@ -286,6 +273,78 @@ function kebabCase(str) {
return str.toLowerCase().replace(' ', '-');
}

function scaffoldSvelte() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Abstracting the scaffolding into the functions is a clean approach.

// `-y` installs the latest version of create-svelte without prompting.
spawnSync('npm', ['create', 'svelte@latest', '-y', 'ui'], {
stdio: 'inherit',
shell: true,
});
sh.cp(
path.join(__dirname, 'ui', 'svelte', 'hooks.server.js'),
path.join('ui', 'src')
);
}

function scaffoldNext() {
// https://nextjs.org/docs/api-reference/create-next-app#options
spawnSync('npx', ['create-next-app@latest', 'ui', '--use-npm'], {
stdio: 'inherit',
shell: true,
});
sh.rm('-rf', path.join('ui', '.git')); // Remove NextJS' .git; we will init .git in our monorepo's root.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we init .git in the mono repo yet?

// Read in the NextJS config file and add the middleware.
const nextConfig = fs.readFileSync(path.join('ui', 'next.config.js'), 'utf8');
const newNextConfig = nextConfig.replace(
/^}(.*?)$/gm, // Search for the last '}' in the file.
`
async headers() {
return [
{
source: '/(.*)',
headers: [
{
key: 'Cross-Origin-Opener-Policy',
value: 'same-origin',
},
{
key: 'Cross-Origin-Embedder-Policy',
value: 'require-corp',
},
],
},
];
}
};`
);
fs.writeFileSync(path.join('ui', 'next.config.js'), newNextConfig);
}

function scaffoldNuxt() {
console.log(" Choose 'no version control' when prompted.");
spawnSync('npx', ['create-nuxt-app', 'ui'], {
stdio: 'inherit',
shell: true,
});
if (fs.existsSync(path.join('ui', '.git'))) {
sh.rm('-rf', path.join('ui', '.git')); // Remove NuxtJS' .git; we will init .git in our monorepo's root.
}
sh.mkdir(path.join('ui', 'middleware'));
sh.cp(
path.join(__dirname, 'ui', 'nuxt', 'headers.js'),
path.join('ui', 'middleware')
);

// Read in the NuxtJS config file and add the middleware.
const nuxtConfig = fs.readFileSync(path.join('ui', 'nuxt.config.js'), 'utf8');
const newNuxtConfig = nuxtConfig.replace(
/^}(.*?)$/gm, // Search for the last '}' in the file.
`
,serverMiddleware: ['middleware/headers']
};`
);
fs.writeFileSync(path.join('ui', 'nuxt.config.js'), newNuxtConfig);
}

module.exports = {
project,
step,
Expand Down
7 changes: 7 additions & 0 deletions src/lib/ui/nuxt/headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// To properly enable SnarkyJS for the web, we must set the COOP and COEP headers.
// See here for more information: https://docs.minaprotocol.com/zkapps/how-to-write-a-zkapp-ui#enabling-coop-and-coep-headers
export default function (req, res, next) {
res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
next();
}
9 changes: 9 additions & 0 deletions src/lib/ui/svelte/hooks.server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// To properly enable SnarkyJS for the web, we must set the COOP and COEP headers.
// See here for more information: https://docs.minaprotocol.com/zkapps/how-to-write-a-zkapp-ui#enabling-coop-and-coep-headers
/** @type {import('@sveltejs/kit').Handle} */
export async function handle({ event, resolve }) {
const response = await resolve(event);
response.headers.set('Cross-Origin-Opener-Policy', 'same-origin');
response.headers.set('Cross-Origin-Embedder-Policy', 'require-corp');
return response;
}