LogoLogo
  • Technical Direction
  • Technical overview
    • Technical Implementation
    • API Design Guide
      • Data Definitions and Standards
      • Data Transfer Objects
      • Documentation
      • Environments
      • Error Handling
      • Example API Service
      • GraphQL Naming Conventions
      • Methods
      • Naming Conventions
      • Once Only Principle
      • Pagination
      • Resource Oriented Design
      • REST Request
      • REST Response
      • Security
      • Versioning
    • Ísland.is Public Web Data Flow
    • Code Reviews
    • Code Standards
    • Monorepo
    • Project Management
    • Teamwork
    • Architectural Decision Records
      • Use Markdown Architectural Decision Records
      • Use NX
      • Continuous Integration
      • CSS
      • Branching and Release Strategy
      • Error Tracking and Monitoring
      • What API Management Tool to Consider
      • Viskuausan Static Site Generator
      • Use OAuth 2.0 and OpenID Connect As Protocols for Authentication and Authorization
      • Unified Naming Strategy for Files and Directories
      • CMS
      • Open Source License
      • What Chart Library Should We Use Across Island.is?
      • What Feature Flag Service/application Should We Use at Island.is?
      • Logging, Monitoring and APM Platform
      • ADR Template
    • Log Management Policy
  • Products
    • Island.is Authentication Service
      • Terminology
      • Integration Options
      • Authentication Flows
      • Authorising API Endpoints
      • Session Lifecycle
      • Scopes and Tokens
      • Delegations
      • Configuration
      • Tools and Examples
      • Environments
      • Test IAS with Postman
      • Using the IAS admin portal
    • Notifications / Hnipp
      • New Notification Setup Guide
      • Notifications service workflow overview
      • Email notifications
    • Pósthólfið
      • Security Checklist
      • Introduction
      • Skjalatilkynning API
      • Skjalaveita API
      • Sequence Diagram
      • Interfaces
    • Straumurinn (X-Road)
      • Architecture Guidelines for Service Providers and Consumers
      • Setting up an X-Road Security Server
        • Network Configuration
      • X-Road - Uppfærsla á öryggisþjónum
      • Straumurinn - Notkun og umsýsla
      • X-Road Central - current version
  • Development
    • Getting Started
    • Generating a New Project
    • Definition of done
    • Devops
      • Continuous Delivery
      • Database
      • Dockerizing
      • Environment Setup
      • Logging
      • Metrics
      • NextJS Custom Server
      • Observability
      • Operations Base Principles
      • Security
      • Service Configuration
      • Support
    • AWS Secrets
    • Feature Flags
    • Documentation Contributions
    • Defining Monorepo Boundaries With Tags
    • OpenAPI
    • Code Generation
    • Workspace Settings (Deprecated)
    • External Contributions
  • REFERENCE
    • Problems
      • 400 Validation Failed
      • 400 Attempt Failed
      • 403 Bad Subject
      • 400 500 Template API Error
    • Glossary
  • Misc
    • Guide: Adding a Payment Step to an Application
    • Guide: Enable Organisations to Make Requests to an Application
    • README Template
Powered by GitBook
On this page

Was this helpful?

  1. Development
  2. Devops

NextJS Custom Server

PreviousMetricsNextObservability

Last updated 1 year ago

Was this helpful?

We use a custom NextJS server to standardise logging, tracing and metrics in production. This uses NX's official support for .

Setup in new project

Follow these steps to configure our custom NextJS server in your NextJS project:

  1. Add a server.ts file to the root folder of your NextJS project with the following content. Be sure to replace the {{variables}} with correct values. See apps/web/server.ts for example setup.

import { bootstrap } from '@island.is/infra-next-server'

bootstrap({
  name: '{{projectName}}',
  appDir: 'apps/{{pathToAppDir}}',
})
  1. Then create a new tsconfig file called tsconfig.server.json with the following contents. Be sure to replace the {{variables}} with correct values.

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "module": "commonjs",
    "noEmit": false,
    "incremental": true,
    "tsBuildInfoFile": "../../{{extraRelativePathToRoot}}tmp/buildcache/apps/{{pathToAppDir}}/server",
    "types": [
      "node"
    ]
  },
  "include": ["server.ts"]
}
  1. Finally, open the project.json file in your project folder and replace the "build" and "serve" target keys with the following content. Remember to replace the {{variables}} with correct values, and edit the build configurations as needed.

"build": {
  "executor": "@nx/next:build",
  "outputs": ["{options.outputPath}"],
  "defaultConfiguration": "production",
  "options": {
    "outputPath": "dist/apps/{{pathToAppDir}}"
  },
  "configurations": {
    "development": {
      "outputPath": "apps/{{pathToAppDir}}"
    },
    "production": {}
  },
  "dependsOn": [
    "build-custom-server"
  ]
},
"build-custom-server": {
  "executor": "@nx/webpack:webpack",
  "defaultConfiguration": "production",
  "options": {
    "outputPath": "dist/apps/{{pathToAppDir}}",
    "main": "apps/{{pathToAppDir}}/server.ts",
    "tsConfig": "apps/{{pathToAppDir}}/tsconfig.server.json",
    "maxWorkers": 2,
    "assets": [],
    "compiler": "tsc",
    "target": "node"
  },
  "configurations": {
    "development": {},
    "production": {
      "optimization": true,
      "extractLicenses": true,
      "inspect": false
    }
  }
},
"serve": {
  "executor": "@nx/next:server",
  "defaultConfiguration": "development",
  "options": {
    "buildTarget": "{{projectName}}:build",
    "dev": true,
    "customServerTarget": "{{projectName}}:serve-custom-server"
  },
  "configurations": {
    "development": {
      "buildTarget": "{{projectName}}:build:development",
      "dev": true,
      "customServerTarget": "{{projectName}}:serve-custom-server:development"
    },
    "production": {
      "buildTarget": "{{projectName}}:build:production",
      "dev": false,
      "customServerTarget": "{{projectName}}:serve-custom-server:production"
    }
  }
},
"serve-custom-server": {
  "executor": "@nx/js:node",
  "defaultConfiguration": "development",
  "options": {
    "buildTarget": "{{projectName}}:build-custom-server"
  },
  "configurations": {
    "development": {
      "buildTarget": "{{projectName}}:build-custom-server:development"
    },
    "production": {
      "buildTarget": "{{projectName}}:build-custom-server:production"
    }
  }
},
custom servers in NextJS