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.
| Endpoint | Purpose | Result |
|---|---|---|
/api/export/image | Capture the timetable as an image | My Timetable.png |
/api/export/pdf | Print the timetable as a document | My 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 group | Role in the rendered view |
|---|---|
| Semester and timetable | Identify the term and lesson choices to show |
| Colors | Preserve each module's display color |
| Hidden and teaching-assistant modules | Preserve visibility and module role choices |
| Theme and color scheme | Preserve 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
ExportDatachanges. - 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.setDatahook and the#timetable-onlycapture 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
| Trigger | Consequence | Guard |
|---|---|---|
The data query value is missing, malformed, or outside the supported shape | The service returns a 422 page instead of opening a browser | Validate the request before browser startup |
| The academic-year setting is absent or has the wrong form | The service returns a 500 error before it can load module data | Set a valid academic-year value for the deployment |
| The configured timetable page cannot be opened | Image and PDF requests fail before state injection | Keep the page URL or built page reachable from the export runtime |
| No usable Chrome or Chromium executable is available | The browser cannot start and no file is produced | Provide the serverless browser package or a supported executable |
| The browser control package and Chromium package are not compatible | Browser startup or page capture can fail before a file is returned | Upgrade the paired browser dependencies together |
| A selected module is absent from both local and public data sources | The rendered timetable can omit that module's lesson details | Keep module data current and preserve access to the public fallback |
The timetable-only page lacks #timetable-only | Image capture cannot determine the content boundary | Keep the export entry point and capture element in sync |
| Browser rendering takes longer than the hosted function limit | The request ends without a completed download | Keep the render path within the function duration configured for both endpoints |
Key files
| File | Why to read it |
|---|---|
| website/…/export.ts | Shows how the website selects an export endpoint and builds its request URL. |
| website/…/export.ts | Shows which browser state crosses the service boundary. |
| website/…/export.ts | Defines the website-side export contract. |
| website/…/main.tsx | Shows how the standalone page receives data and applies display mode. |
| website/…/TimetableOnly.tsx | Shows the read-only timetable view used for capture. |
| export/…/image.ts | Defines the image endpoint and its viewport options. |
| export/…/pdf.ts | Defines the PDF endpoint and response format. |
| export/…/handler.ts | Defines the shared validation, browser, cleanup, and error flow. |
| export/…/data.ts | Defines request validation and module data preparation. |
| export/…/render-serverless.ts | Defines serverless browser selection and PNG or PDF capture. |
| export/…/config.ts | Defines the settings that connect the service to its page and data sources. |
| export/…/views.ts | Maps service errors to the user-facing error views. |
| export/package.json | Defines the browser runtime packages and the local and hosted service commands. |
| export/vercel.json | Defines the hosted function limits and export deployment routes. |