Introducing a new way to review your PRs.

Read the announcement

nusmodifications / nusmods

🏫 Official course planning platform for National University of Singapore.

View on GitHub

20 documents · Updated Nov 6, 2026

docs / timetable-export-service

Timetable export service

The timetable export service is a standalone application that turns a timetable snapshot from the website into a PNG image or PDF file. It rebuilds the read-only timetable in a server-side browser, so users can download the same schedule view without running a second timetable editor; Export and operations gives the wider delivery context, while Deployment and hosting covers the hosted runtime.

Export boundary

The service owns file delivery rather than timetable editing. The website prepares a small request from the current browser state, and the export service validates that request before it starts the more costly browser work.

Public export API

The API accepts GET requests with timetable data in the data query parameter. Each endpoint returns a file attachment with a stable name and content type.

EndpointPurposeResult
/api/export/imageCapture the timetable as an imageMy Timetable.png
/api/export/pdfPrint the timetable as a documentMy Timetable.pdf

Image requests can also carry a pixel ratio and a positive viewport size. The service clamps the pixel ratio to the supported range and uses its default viewport when the size is not valid.

References: export/…/image.ts• L8-44 export/…/pdf.ts• L6-17 export/…/render-serverless.ts• L12-24

Export data contract

ExportData is the portable view state needed to reproduce one semester timetable. The website extracts the active semester, selected lessons, module colors, hidden modules, teaching-assistant modules, theme, and color scheme before it builds the request URL.

The contract keeps export independent from a user session or a server-side timetable record. The export application has a matching type and validation rule set, so a request can be rejected before it opens a browser when its shape is not safe to render.

Data groupRole in the rendered view
Semester and timetableIdentify the term and lesson choices to show
ColorsPreserve each module's display color
Hidden and teaching-assistant modulesPreserve visibility and module role choices
Theme and color schemePreserve title, orientation, theme, and light or dark mode

References: website/…/export.ts• L8-28 website/…/export.ts• L6-16 export/…/types.ts• L56-67 export/…/data.ts• L77-121

Request lifecycle

The request lifecycle moves from a browser link to validated state, then to a short-lived browser render and a file response. The same shared handler protects both output formats.

Client request preparation

The website's website/…/export.ts is the first meaningful boundary for an export request. It serializes the extracted state as JSON inside a query parameter and selects the image or PDF endpoint; the timetable download menu supplies the current semester, timetable, display scheme, and device pixel ratio for image output.

The service then fetches module records for the module codes in the timetable. It enriches those records with lesson lookup data so the timetable-only page can turn the compact lesson choices into visible lesson details.

This diagram shows how browser state becomes a downloadable file.

flowchart TD
    A[Selected timetable in website] --> B[Extract export state]
    B --> C[Serialize as data query]
    C --> D[Image or PDF endpoint]
    D --> E[Parse and validate request]
    E --> F[Open timetable-only page]
    F --> G[Load module details]
    G --> H[Inject state into read-only view]
    H --> I[Capture PNG or PDF]
    I --> J[Send file attachment]

References: website/…/export.ts• L16-43 website/…/export.ts• L8-28 website/…/ExportMenu.tsx• L83-108 export/…/data.ts• L39-57

Shared handler flow

The serverless handler checks the academic-year setting and error reporting setup before it parses request data. It validates the request before browser startup, which prevents malformed links from consuming a browser session.

After validation, the handler opens a page, calls the format-specific renderer, and closes the browser in a finally step. Invalid input becomes a 422 response with a user-facing error view. Browser startup or render failures become a 500 response and are reported when error reporting is configured.

References: export/…/handler.ts• L10-32 export/…/handler.ts• L40-92 export/…/image.ts• L13-44 export/…/pdf.ts• L6-17

Server-side timetable rendering

The renderer uses a real Chromium page to run the same timetable display used by the website. This preserves layout and theme behavior while keeping the export result read-only and bounded to the supplied request.

Browser selection

The serverless path selects a Chrome or Chromium executable before it creates a page. It first uses the serverless Chromium package and then checks platform-specific browser paths when needed. The browser runs without a visible window and opens the configured timetable page before export begins.

The export application uses Puppeteer Core with the serverless Chromium package as its browser control path. Its package scripts support a built service process and a local development process. The hosted endpoints use the same render path, with deployment limits described in Deployment and hosting.

The page source can be a hosted URL or a built local page, depending on the configured deployment mode. In both cases, the page must load the export entry point and expose the interface used to receive the request data.

References: export/…/render-serverless.ts• L26-47 export/…/chrome-executable.ts• L3-32 export/…/config.ts• L1-16 export/package.json• L5-35

Request-data preparation

Before capture, the renderer loads module data for every selected module code. A configured local data source is preferred, with the public NUSMods module API as a fallback when local data is not available. A module that cannot be loaded is omitted from the data passed to the page.

The renderer sends the module records and the validated export state to the page through window.setData. It waits for the page to apply that state, then measures the timetable element so image capture follows the visible timetable rather than the full browser viewport.

References: export/…/data.ts• L12-57 export/…/render-serverless.ts• L50-67 export/…/makeModuleLessonMap.ts• L125-129

Timetable-only view

The export page is a small browser entry point that renders the shared timetable content in read-only mode. It applies the requested theme and color scheme, keeps the supplied semester and display choices, and does not expose the normal timetable editing shell.

The page starts with an empty view, then updates its store and view when the renderer calls window.setData. The #timetable-only element is the capture boundary for image output and the visual content used by PDF output.

References: website/…/main.tsx• L18-60 website/…/TimetableOnly.tsx• L15-49 website/…/index.html• L1-12

Output views

The service has two file views and a small set of error views. File views preserve the timetable's display choices while using output-specific capture rules.

Image output

Image output captures the measured timetable element as a PNG. The requested pixel ratio changes the device scale, while valid width and height values change the capture viewport so the result can fit the caller's display needs.

The response marks the PNG as an attachment. This makes the export link behave as a download instead of replacing the timetable page in the user's browser.

References: export/…/image.ts• L18-44 export/…/render-serverless.ts• L69-80

PDF output

PDF output uses the same injected timetable but prints it on A4 paper with the screen styles and background colors. The requested timetable orientation selects portrait or landscape output, so the document follows the layout chosen in the website.

The response marks the PDF as an attachment with a stable file name. This keeps PDF delivery separate from the page that the browser rendered for capture.

References: export/…/pdf.ts• L12-17 export/…/render-serverless.ts• L82-93 website/…/export.ts• L6-15

Error views

Error views give malformed requests and unexpected service failures different outcomes. A 422 page tells the user that the export link cannot be understood. A 500 page reports that the service failed and can include an error-report form when an event was recorded. The local application also renders a 404 page for routes that are not export routes.

References: export/…/handler.ts• L80-92 export/…/views.ts• L4-14 export/…/422.pug• L1-8 export/…/500.pug• L1-15 export/…/app.ts• L57-83

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the website export extraction, the duplicated service type, and server-side validation aligned when ExportData changes.
  • Keep lesson identifiers and module lesson maps compatible when lesson selection or module data shape changes.
  • Keep theme, color-scheme, and orientation handling aligned between window.setData, the timetable-only view, and PDF capture.
  • Keep the configured page artifact or URL able to load the window.setData hook and the #timetable-only capture element.
  • Keep browser startup, viewport limits, and hosted function duration compatible when render cost or output size changes.
  • Keep image and PDF response headers aligned with the output format when endpoint behavior changes.
  • Keep the serverless Chromium and Puppeteer Core package versions compatible when browser dependencies change.

Important failure modes

TriggerConsequenceGuard
The data query value is missing, malformed, or outside the supported shapeThe service returns a 422 page instead of opening a browserValidate the request before browser startup
The academic-year setting is absent or has the wrong formThe service returns a 500 error before it can load module dataSet a valid academic-year value for the deployment
The configured timetable page cannot be openedImage and PDF requests fail before state injectionKeep the page URL or built page reachable from the export runtime
No usable Chrome or Chromium executable is availableThe browser cannot start and no file is producedProvide the serverless browser package or a supported executable
The browser control package and Chromium package are not compatibleBrowser startup or page capture can fail before a file is returnedUpgrade the paired browser dependencies together
A selected module is absent from both local and public data sourcesThe rendered timetable can omit that module's lesson detailsKeep module data current and preserve access to the public fallback
The timetable-only page lacks #timetable-onlyImage capture cannot determine the content boundaryKeep the export entry point and capture element in sync
Browser rendering takes longer than the hosted function limitThe request ends without a completed downloadKeep the render path within the function duration configured for both endpoints

Key files

FileWhy to read it
website/…/export.tsShows how the website selects an export endpoint and builds its request URL.
website/…/export.tsShows which browser state crosses the service boundary.
website/…/export.tsDefines the website-side export contract.
website/…/main.tsxShows how the standalone page receives data and applies display mode.
website/…/TimetableOnly.tsxShows the read-only timetable view used for capture.
export/…/image.tsDefines the image endpoint and its viewport options.
export/…/pdf.tsDefines the PDF endpoint and response format.
export/…/handler.tsDefines the shared validation, browser, cleanup, and error flow.
export/…/data.tsDefines request validation and module data preparation.
export/…/render-serverless.tsDefines serverless browser selection and PNG or PDF capture.
export/…/config.tsDefines the settings that connect the service to its page and data sources.
export/…/views.tsMaps service errors to the user-facing error views.
export/package.jsonDefines the browser runtime packages and the local and hosted service commands.
export/vercel.jsonDefines the hosted function limits and export deployment routes.