Source code for the 2021 redesign of cal-adapt.org.
This project was bootstrapped with the Sapper framework for SvelteJS.
This project uses Node 14. Run the following command before installing or running your development environment:
nvm use 14
Once you have cloned the project, install dependencies and run the project in development mode:
cd cal-adapt-website-2021
npm install # or yarn
npm run dev
The project should be viewable in your browser at http://localhost:3000
.
First, make the deploy
script is executable in your environment:
# on unix systems:
chmod +x scripts/deploy.mjs
Then run the appropriate deploy script for the environment on which you would like to deploy to:
# e.g. for dev.cal-adapt.org
npm run deploy-dev
This will first run sapper export
and then transfer the output to the appropriate location on the Cal-Adapt webserver.
You may deploy a local build to be hosted on Netlify for sharing a new feature or bug fix in isolation.
First install the Netlify CLI tools:
npm install netlify-cli -g
Then log-in to Netlify:
netlify login
Set the appropriate environment variables prior to running the deploy script:
# the netlify personal access token:
export NETLIFY_AUTH_TOKEN="xxxxxxxxx"
# the subdomain for the deployment, e.g.
export NETLIFY_ALIAS="bug-fix-abc"
And then deploy as follows:
npm run deploy-netlify
If you would like to view the build locally prior to deploying, first create the build without transfering it to the server:
npm run deploy-dev -- --transfer=false
Then run a local server that will serve the contents of __sapper__/export
:
npm run start:export
You may then view the built site on http://localhost:5000
.
This can be useful when debugging issues for the production environment.
Unit tests are run using Jest and @testing-library/svelte.
To run all tests:
npm run test
To run tests in watch mode:
npm run test:watch
To run tests for a specific file:
npm run test -- <pattern>
...where <pattern>
is a regex for a specific file or test name.
For help on writing tests for Svelte components see the following resources:
The featureFlags
JSON document contains environment variables that are used to enable or disable various features or routes of the website. For client-side code these varaibles are accessible under process.env
and in server side code (e.g. Sapper's preloading) they are accessible under process.cal_adapt_features
. They differ due to how process.env
is handled by browser (client) and NodeJS (server side) environments with Webpack.
Note that the values of feature flags will differ depending on the deploy environment that is set when deploying the app (e.g. the location being deployed to such as dev.cal-adapt.org
, beta.cal-adapt.org
, or cal-adapt.org
). The deploy script sets an environment variable DEPLOY
which is used in the webpack.config.js
to set the values of feature flags. During local development the value of the DEPLOY
env variable will fallback to dev
(the same as deploying to dev.cal-adapt.org
).
To inspect the bundled JavaScript code first run the dev
script:
npm run dev
Then open your browser to http://127.0.0.1:8888
or whatever address is outputted by Webpack Bundle Analyzer in the terminal.
The src directory contains the entry points for the app — client.js
, server.js
and (optionally) a service-worker.js
— along with a template.html
file and a routes
directory.
This is the heart of the app. There are two kinds of routes — pages, and server routes.
Pages are Svelte components written in .svelte
files. When a user first visits the application, they will be served a server-rendered version of the route in question, plus some JavaScript that 'hydrates' the page and initialises a client-side router. From that point forward, navigating to other pages is handled entirely on the client for a fast, app-like feel. (Sapper will preload and cache the code for these subsequent pages, so that navigation is instantaneous.)
Server routes are modules written in .js
files, that export functions corresponding to HTTP methods. Each function receives Express request
and response
objects as arguments, plus a next
function. This is useful for creating a JSON API, for example.
There are three simple rules for naming the files that define your routes:
- A file called
src/routes/about.svelte
corresponds to the/about
route. A file calledsrc/routes/blog/[slug].svelte
corresponds to the/blog/:slug
route, in which caseparams.slug
is available to the route - The file
src/routes/index.svelte
(orsrc/routes/index.js
) corresponds to the root of your app.src/routes/about/index.svelte
is treated the same assrc/routes/about.svelte
. - Files and directories with a leading underscore do not create routes. This allows you to colocate helper modules and components with the routes that depend on them — for example you could have a file called
src/routes/_helpers/datetime.js
and it would not create a/_helpers/datetime
route.
This directory is managed by Sapper and generated when building. It contains all the code you import from @sapper
modules.
The static directory contains static assets that should be served publicly. Files in this directory will be available directly under the root URL, e.g. an image.jpg
will be available as /image.jpg
.
The default service-worker.js will preload and cache these files, by retrieving a list of files
from the generated manifest:
import { files } from "@sapper/service-worker";
If you have static files you do not want to cache, you should exclude them from this list after importing it (and before passing it to cache.addAll
).
Static files are served using sirv.
The files
directory is used to host pdfs, Word docs or other files that CEC wants Cal-Adapt to host and are typically not accessible elsewhere on the web. This directory which would normally be in the static
directory (similar to the static/img
or static/data
) is not checked into this github repo.
Files that need to be hosted are directly copied to the Cal-Adapt server at /var/www/cal-adapt.org/files
. These files can be linked on blogs/events/other content pages as needed e.g. /files/01_Memo_Evaluation_of_Downscaled_GCMs_Using_WRF_CEC_final.pdf
. Note: Filenames should not have blank spaces.
Webpack is used to provide code-splitting and dynamic imports, as well as compiling Svelte components.
When using Svelte components installed from npm, such as @sveltejs/svelte-virtual-list, Svelte needs the original component source (rather than any precompiled JavaScript that ships with the component). This allows the component to be rendered server-side, and also keeps your client-side app smaller.
Because of that, it's essential that the bundler doesn't treat the package as an external dependency. Install the package to devDependencies
rather than dependencies
, which will cause it to get bundled (and therefore compiled) with your app:
npm install -D @sveltejs/svelte-virtual-list
This project began as a Svelte Sapper template and borrows some code from both the Svelte and Sapper documentation, both of which are copyright 2016 – present, Svelte and Sapper contributors under the MIT License.
Some code relating to Charting components for the dashboards has been borrowed from the Svelte Layer Cake library, copyright 2021 Michael Keller under the MIT License.
Some code relating to the Map Legend component was borrowed from the ObservableHQ Color-Legend developed by Mike Bostock, copyright 2019–2020 Observable, Inc. under the ISC License.