-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add OpenNextjs preset #179
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
Merged
+153
−0
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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,58 @@ | ||
import type { AzionBuild, AzionConfig } from 'azion/config'; | ||
|
||
const config: AzionConfig = { | ||
build: { | ||
entry: '.open-next/worker.js', | ||
polyfills: true, | ||
bundler: 'esbuild', | ||
preset: 'opennextjs', | ||
} as AzionBuild, | ||
origin: [ | ||
{ | ||
name: 'origin-storage-default', | ||
type: 'object_storage', | ||
}, | ||
], | ||
functions: [ | ||
{ | ||
name: 'handler', | ||
path: '.edge/functions/handler.js', | ||
}, | ||
], | ||
rules: { | ||
request: [ | ||
{ | ||
name: 'Set Storage Origin for All Requests', | ||
match: '^\\/_next\\/static\\/', // starts with '/_next/static/' | ||
behavior: { | ||
setOrigin: { | ||
name: 'origin-storage-default', | ||
type: 'object_storage', | ||
}, | ||
deliver: true, | ||
}, | ||
}, | ||
{ | ||
name: 'Deliver Static Assets', | ||
match: '.(css|js|ttf|woff|woff2|pdf|svg|jpg|jpeg|gif|bmp|png|ico|mp4|json)$', | ||
behavior: { | ||
setOrigin: { | ||
name: 'origin-storage-default', | ||
type: 'object_storage', | ||
}, | ||
deliver: true, | ||
}, | ||
}, | ||
{ | ||
name: 'Execute Edge Function', | ||
match: '^/', | ||
behavior: { | ||
runFunction: 'handler', | ||
forwardCookies: true, | ||
}, | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
export default config; |
This file contains hidden or 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,6 @@ | ||
import type { AzionBuildPreset } from 'azion/config'; | ||
import config from './config'; | ||
import metadata from './metadata'; | ||
import prebuild from './prebuild'; | ||
|
||
export const opennextjs: AzionBuildPreset = { config, metadata, prebuild }; |
This file contains hidden or 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 @@ | ||
import type { PresetMetadata } from 'azion/config'; | ||
|
||
const metadata: PresetMetadata = { | ||
name: 'opennextjs', | ||
}; | ||
|
||
export default metadata; |
This file contains hidden or 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,80 @@ | ||
import { BuildConfiguration, BuildContext } from 'azion/config'; | ||
import { exec } from 'azion/utils/node'; | ||
import { readFile } from 'fs/promises'; | ||
import path from 'path'; | ||
|
||
/** | ||
* Runs custom prebuild actions for OpenNextjs | ||
*/ | ||
async function prebuild(buildConfig: BuildConfiguration, ctx: BuildContext): Promise<void> { | ||
const pkgOpenNextjsName = '@aziontech/opennextjs-azion'; | ||
const openNextjsCommand = 'npm exec opennextjs-azion'; | ||
|
||
// Check if the OpenNextjs Azion is installed | ||
const isOpenNextjsInstalled = await checkIfOpenNextjsIsInstalled(); | ||
if (!isOpenNextjsInstalled) { | ||
const packageManager = await indentifyPackageManager(); | ||
const execCommand = | ||
packageManager === 'yarn' | ||
? `yarn add -D ${pkgOpenNextjsName}` | ||
: packageManager === 'pnpm' | ||
? `pnpm add -D ${pkgOpenNextjsName}` | ||
: `npm i -D ${pkgOpenNextjsName}`; | ||
await exec(execCommand, { | ||
scope: 'OpenNextjs', | ||
verbose: true, | ||
interactive: true, | ||
}); | ||
} | ||
// Run OpenNextjs command build | ||
if (ctx.production || !ctx.skipProjectBuild) { | ||
const skipBuild = ctx.skipProjectBuild ? '--skipBuild' : ''; | ||
await exec(`${openNextjsCommand} build -- ${skipBuild}`, { | ||
scope: 'OpenNextjs', | ||
verbose: true, | ||
interactive: true, | ||
}); | ||
} | ||
|
||
// Run OpenNextjs commands to populate assets | ||
if (ctx.production) { | ||
await exec(`${openNextjsCommand} populateAssets`, { | ||
scope: 'OpenNextjs', | ||
verbose: true, | ||
}); | ||
} | ||
|
||
// Run OpenNextjs commands to populate cache | ||
if (ctx.production) { | ||
await exec(`${openNextjsCommand} populateCache`, { | ||
scope: 'OpenNextjs', | ||
verbose: true, | ||
}); | ||
} | ||
} | ||
|
||
async function checkIfOpenNextjsIsInstalled(): Promise<boolean> { | ||
const packageJsonPath = path.resolve(process.cwd(), 'package.json'); | ||
const packageJson = await readFile(packageJsonPath, 'utf-8'); | ||
const packageJsonObj = JSON.parse(packageJson); | ||
if (!packageJsonObj.devDependencies || !packageJsonObj.devDependencies['@aziontech/opennextjs-azion']) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
async function indentifyPackageManager(): Promise<string> { | ||
const lockFiles = ['package-lock.json', 'yarn.lock', 'pnpm-lock.yaml']; | ||
for (const lockFile of lockFiles) { | ||
const filePath = path.resolve(process.cwd(), lockFile); | ||
try { | ||
await readFile(filePath, 'utf-8'); | ||
return lockFile.includes('yarn') ? 'yarn' : lockFile.includes('pnpm') ? 'pnpm' : 'npm'; | ||
} catch (error) { | ||
// File not found, continue to the next one | ||
} | ||
} | ||
return 'npm'; // Default to npm if no lock file is found | ||
} | ||
|
||
export default prebuild; |
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.
Is this
All Requests
orStatic
requests only? 🤔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.
Everything starting with '/_next/static/' is in the match field. Thanks, I'll improve the name of this rule.