Skip to content

Commit

Permalink
fix: don't crash if prerender: false (#101)
Browse files Browse the repository at this point in the history
* chore: repro

* fix: check if prerendered-routes.json exists

* fix: install deps in fixture before testing

* fix: use local plugin version
  • Loading branch information
Skn0tt committed Mar 4, 2024
1 parent 1f324f5 commit a72519b
Show file tree
Hide file tree
Showing 29 changed files with 13,179 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"pretest:fixtures:missing-angular-deps": "cd tests/fixtures/missing-angular-deps && npm ci",
"pretest:fixtures:application-builder": "cd tests/fixtures/application-builder && npm ci",
"pretest:fixtures:browser-builder": "cd tests/fixtures/browser-builder && npm ci",
"pretest:fixtures:prerender-false": "cd tests/fixtures/prerender-false && npm ci",
"pretest": "run-s pretest:*",
"test": "node --test"
},
Expand Down
16 changes: 13 additions & 3 deletions src/helpers/setUpEdgeFunction.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { Buffer } = require('node:buffer')
const { existsSync, readdirSync } = require('node:fs')
const { readdirSync, existsSync } = require('node:fs')
const { writeFile, mkdir, readFile } = require('node:fs/promises')
const { join, relative, sep, posix } = require('node:path')
const process = require('node:process')
Expand Down Expand Up @@ -46,6 +46,17 @@ const getProject = (angularJson, failBuild) => {

module.exports.getProject = getProject

/**
* @param {string} outputDir
* @returns {Promise<string[]>}
*/
const getPrerenderedRoutes = async (outputDir) => {
const file = join(outputDir, 'prerendered-routes.json')
if (!existsSync(file)) return []
const { routes: prerenderedRoutes } = await readJson(file)
return prerenderedRoutes
}

const setUpEdgeFunction = async ({ angularJson, constants, failBuild }) => {
const project = getProject(angularJson)
const {
Expand All @@ -72,8 +83,7 @@ const setUpEdgeFunction = async ({ angularJson, constants, failBuild }) => {
(path) => `/${relative(join(outputDir, 'browser'), path)}`,
)

const { routes: prerenderedRoutes } = await readJson(join(outputDir, 'prerendered-routes.json'))
const excludedPaths = [...staticFiles, ...prerenderedRoutes].map(toPosix)
const excludedPaths = [...staticFiles, ...(await getPrerenderedRoutes(outputDir))].map(toPosix)

// buy putting this into a separate module that's imported first,
// we ensure this is initialised before any other module
Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/prerender-false/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Editor configuration, see https://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.ts]
quote_type = single

[*.md]
max_line_length = off
trim_trailing_whitespace = false
44 changes: 44 additions & 0 deletions tests/fixtures/prerender-false/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

# Compiled output
/dist
/tmp
/out-tsc
/bazel-out

# Node
/node_modules
npm-debug.log
yarn-error.log

# IDEs and editors
.idea/
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# Visual Studio Code
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history/*

# Miscellaneous
/.angular/cache
.sass-cache/
/connect.lock
/coverage
/libpeerconnection.log
testem.log
/typings

# System files
.DS_Store
Thumbs.db

.netlify
95 changes: 95 additions & 0 deletions tests/fixtures/prerender-false/.netlify/plugins/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/fixtures/prerender-false/.netlify/plugins/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "netlify-local-plugins",
"description": "This directory contains Build plugins that have been automatically installed by Netlify.",
"version": "1.0.0",
"private": true,
"author": "Netlify",
"license": "MIT",
"dependencies": {
"@netlify/angular-runtime": "2.0.5"
}
}
27 changes: 27 additions & 0 deletions tests/fixtures/prerender-false/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# PrerenderFalse

This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 17.2.2.

## Development server

Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The application will automatically reload if you change any of the source files.

## Code scaffolding

Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`.

## Build

Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory.

## Running unit tests

Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io).

## Running end-to-end tests

Run `ng e2e` to execute the end-to-end tests via a platform of your choice. To use this command, you need to first add a package that implements end-to-end testing capabilities.

## Further help

To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI Overview and Command Reference](https://angular.io/cli) page.
100 changes: 100 additions & 0 deletions tests/fixtures/prerender-false/angular.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"prerender-false": {
"projectType": "application",
"schematics": {},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:application",
"options": {
"outputPath": "dist/prerender-false",
"index": "src/index.html",
"browser": "src/main.ts",
"polyfills": [
"zone.js"
],
"tsConfig": "tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css"
],
"scripts": [],
"server": "src/main.server.ts",
"prerender": false,
"ssr": {
"entry": "server.ts"
}
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
}
],
"outputHashing": "all"
},
"development": {
"optimization": false,
"extractLicenses": false,
"sourceMap": true
}
},
"defaultConfiguration": "production"
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"buildTarget": "prerender-false:build:production"
},
"development": {
"buildTarget": "prerender-false:build:development"
}
},
"defaultConfiguration": "development"
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"buildTarget": "prerender-false:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"polyfills": [
"zone.js",
"zone.js/testing"
],
"tsConfig": "tsconfig.spec.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css"
],
"scripts": []
}
}
}
}
}
}
6 changes: 6 additions & 0 deletions tests/fixtures/prerender-false/netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[build]
command="npm run build"
ignore="exit 1" ## always build, there might be changes in the plugin

[[plugins]]
package="@netlify/angular-runtime"
Loading

0 comments on commit a72519b

Please sign in to comment.