diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml index a2c2aa7..3aac219 100644 --- a/.github/workflows/deployment.yml +++ b/.github/workflows/deployment.yml @@ -2,7 +2,7 @@ name: Build and Deploy on: push: branches: - - master + [fix-bug-refresh-redirects-to-github-404] permissions: contents: write concurrency: diff --git a/package.json b/package.json index 01ff719..61ce43c 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "ng": "ng", "start": "ng serve", "build": "ng build", - "build:gh-pages": "ng build --base-href /team-app/", + "build:gh-pages": "ng build --base-href /team-app/ && node scripts/afterBuild.js", "watch": "ng build --watch --configuration development", "test": "ng test" }, diff --git a/scripts/afterBuild.js b/scripts/afterBuild.js new file mode 100644 index 0000000..b316c97 --- /dev/null +++ b/scripts/afterBuild.js @@ -0,0 +1,25 @@ +const fs = require('fs'); +const path = require('path'); + +// because GH pages doesn't support SPA routing, we need to redirect all 404s to the root +const distPath = path.join(__dirname, '../dist/team-app'); +const indexPath = path.join(distPath, 'index.html'); +const notFoundPath = path.join(distPath, '404.html'); + +fs.copyFileSync(indexPath, notFoundPath); + +const redirectScript = ` + +`; + +let htmlContent = fs.readFileSync(notFoundPath, 'utf8'); + +// Replace the closing body tag with the script followed by the closing body tag +htmlContent = htmlContent.replace('', redirectScript); + +fs.writeFileSync(notFoundPath, htmlContent); diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index d141711..eec2601 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -12,7 +12,7 @@ const routes: Routes = [ ]; @NgModule({ - imports: [RouterModule.forRoot(routes)], + imports: [RouterModule.forRoot(routes, { useHash: true })], exports: [RouterModule], }) export class AppRoutingModule {}