-
-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add SSR support for dev/build (#182)
Closes #21
- Loading branch information
1 parent
687f61e
commit 965ed61
Showing
21 changed files
with
403 additions
and
50 deletions.
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default async function errorHandler(error, event) { | ||
event.res.end('error' + error.toString()); | ||
} |
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
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
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
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,28 @@ | ||
import 'zone.js/node'; | ||
import { | ||
renderApplication, | ||
ɵSERVER_CONTEXT as SERVER_CONTEXT, | ||
} from '@angular/platform-server'; | ||
import { provideFileRouter } from '@analogjs/router'; | ||
import { withEnabledBlockingInitialNavigation } from '@angular/router'; | ||
|
||
import { AppComponent } from './app/app.component'; | ||
import { enableProdMode } from '@angular/core'; | ||
|
||
if (import.meta.env.PROD) { | ||
enableProdMode(); | ||
} | ||
|
||
export default async function render(url: string, document: string) { | ||
const html = await renderApplication(AppComponent, { | ||
appId: 'analog-app', | ||
document, | ||
url, | ||
providers: [ | ||
provideFileRouter(withEnabledBlockingInitialNavigation()), | ||
{ provide: SERVER_CONTEXT, useValue: 'ssr-analog' }, | ||
], | ||
}); | ||
|
||
return html; | ||
} |
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 |
---|---|---|
@@ -1,11 +1,10 @@ | ||
import 'zone.js'; | ||
import { importProvidersFrom } from '@angular/core'; | ||
import { bootstrapApplication } from '@angular/platform-browser'; | ||
import { HttpClientModule } from '@angular/common/http'; | ||
import { provideHttpClient } from '@angular/common/http'; | ||
import { provideFileRouter } from '@analogjs/router'; | ||
|
||
import { AppComponent } from './app/app.component'; | ||
|
||
bootstrapApplication(AppComponent, { | ||
providers: [provideFileRouter(), importProvidersFrom(HttpClientModule)], | ||
providers: [provideFileRouter(), provideHttpClient()], | ||
}).catch((err) => console.error(err)); |
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
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
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
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,26 @@ | ||
import { loadEsmModule } from '@angular-devkit/build-angular/src/utils/load-esm'; | ||
import { NitroConfig } from 'nitropack'; | ||
|
||
import { Options } from './options'; | ||
|
||
export async function buildServer( | ||
options?: Options, | ||
nitroConfig?: NitroConfig | ||
) { | ||
const { createNitro, build, prepare, copyPublicAssets, prerender } = | ||
await loadEsmModule<typeof import('nitropack')>('nitropack'); | ||
|
||
const nitro = await createNitro({ | ||
dev: false, | ||
...nitroConfig, | ||
}); | ||
await prepare(nitro); | ||
await copyPublicAssets(nitro); | ||
await prerender(nitro); | ||
|
||
if (!options?.prerender) { | ||
await build(nitro); | ||
} | ||
|
||
await nitro.close(); | ||
} |
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
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
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
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 @@ | ||
import { eventHandler } from 'h3'; | ||
|
||
export default eventHandler(async (event) => { | ||
if (event.req.url?.startsWith('/api')) { | ||
return $fetch(`${event.req.url?.replace('/api', '')}`); | ||
} | ||
}); |
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,15 @@ | ||
import { eventHandler } from 'h3'; | ||
// @ts-ignore | ||
import { useStorage } from '#imports'; | ||
|
||
export default eventHandler(async (event) => { | ||
// @ts-ignore | ||
const render = (await import('#build/../ssr/main.server.mjs'))['default']; | ||
|
||
// @ts-ignore | ||
const template = await useStorage().getItem(`/assets/public:index.html`); | ||
|
||
const html = await render(event.req.url, template); | ||
|
||
return html; | ||
}); |
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,16 @@ | ||
import { build, mergeConfig, UserConfig } from 'vite'; | ||
import { Options } from '../options'; | ||
|
||
export async function buildSSRApp(config: UserConfig, options?: Options) { | ||
const ssrBuildConfig = mergeConfig(config, { | ||
build: { | ||
ssr: true, | ||
rollupOptions: { | ||
input: options?.entryServer || './src/main.server.ts', | ||
}, | ||
outDir: options?.ssrBuildDir || './dist/ssr', | ||
}, | ||
}); | ||
|
||
await build(ssrBuildConfig); | ||
} |
Oops, something went wrong.