-
Notifications
You must be signed in to change notification settings - Fork 45
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
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1446c3b
Add COOP & COEP headers for SvelteKit and NextJS
jasongitmail 16f9897
Remove Nuxt's .git if a user creates one
MartinMinkov 4a326ce
Add Nuxt option
MartinMinkov fc7d364
Add comments to direct to docs for info on headers
MartinMinkov f22cc42
Use regex to add headers instead of manual config for NextJS
MartinMinkov a713e8f
Use regex to add headers for NuxtJS in similar way to NextJS
MartinMinkov 716a83e
Update comments text
MartinMinkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
|
@@ -286,6 +273,78 @@ function kebabCase(str) { | |
return str.toLowerCase().replace(' ', '-'); | ||
} | ||
|
||
function scaffoldSvelte() { | ||
// `-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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.