Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Enable HTTPS in local development (#1144)

Zack Krida24834b679bdf
diff --git a/.gitignore b/.gitignore
index b614b410d..1aff5cf19 100644
--- a/.gitignore
+++ b/.gitignore
@@ -35,3 +35,6 @@ storybook-static
# Playwright creates these two folders
test-results
test/Default
+
+# Ignore local certificates
+*.pem
diff --git a/README.md b/README.md
index b629a6312..9182d3360 100644
--- a/README.md
+++ b/README.md
@@ -37,8 +37,46 @@ pnpm i18n:get-translations
# Builds and serves assets with hot-reload
pnpm dev
+
+```
+
+### Using HTTPS Locally
+
+To enable SSL support in local development, use the `pnpm dev:secure` command. This requires you to have a private key and certificate in the root of the repository with the following names:
+
+```shell
+localhost+1-key.pem # The private key file
+localhost+1.pem # The certificate file
+```
+
+The easiest way to create these files is with a local development tool called [mkcrt](https://github.com/FiloSottile/mkcert). First make sure you have [mkcert installed](https://github.com/FiloSottile/mkcert#installation) and activated with `mkcert -install`. Then use `mkcert` to create a certificate for `localhost` and for the external IP address used by Nuxt's development process. That command looks like this:
+
+```shell
+mkcert localhost 192.168.50.119
```
+Be sure to replace the IP address in the example with your own. See the next section for how to identify that IP address.
+
+#### Finding your local IP address
+
+You can find the local IP address Nuxt uses by looking at the output of `nuxt dev`. Look in your console for a box of configuration details that looks like this:
+
+```bash
+# ╭────────────────────────────────────────────╮
+# │ │
+# │ Nuxt @ v2.15.8 │
+# │ │
+# │ ▸ Environment: development │
+# │ ▸ Rendering: server-side │
+# │ ▸ Target: server │
+# │ │
+# │ Listening: http://192.168.50.119:8443/ │ # <-- Use this IP Address
+# │ │
+# ╰────────────────────────────────────────────╯
+```
+
+You will need to regenerate the certificate if this IP address changes for any reason, like by enabling a VPN or changing networks.
+
### Docker setup
Alternatively, you can use Docker to build and run the application. You just have to run:
diff --git a/TESTING_GUIDELINES.md b/TESTING_GUIDELINES.md
index b3dfd07a0..93e47d5ae 100644
--- a/TESTING_GUIDELINES.md
+++ b/TESTING_GUIDELINES.md
@@ -12,25 +12,7 @@ Staging is redeployed everytime we merge to the `main` branch, so if you're look
Once you have the application running, you can visit it in your browser at http://localhost:8443.
-You can also access it from other devices in your same network (like a mobile phone) for additional testing. Typically, the address for this will be displayed in the output of the `pnpm dev` command that you ran to start the server. It will look something like `http://192.168.0.123:8443` or `http://10.0.0.45:8443` depending on your local network configuration. If you can't find this in the output you will need to determine your local IP address yourself.
-
-To do this, follow these instructions for getting your computer's local network IP address:
-
-- Windows:
- 1. Find your IP address: https://support.microsoft.com/en-us/windows/find-your-ip-address-in-windows-f21a9bbc-c582-55cd-35e0-73431160a1b9
- 2. Make sure that your Wi-Fi network is set to "private": https://support.microsoft.com/en-US/windows/make-a-wi-fi-network-public-or-private-in-windows-0460117d-8d3e-a7ac-f003-7a0da607448d
-- macOS:
- 1. Open the Network Preferences app
- 2. Select Wi-Fi from the list of network devices
- 3. Your local IP address will be listed below the "Deactivate Wi-Fi" button
-- Linux:
- 1. Follow the instructions for your distro. Most likely the `ip` command will work. Run `ip address show` in your terminal and find your wireless card in the list (probably the second entry). Look for the `inet` line and copy the first 4 groups of numbers for your IP before the `/24`. The line will probably look like this:
- ```
- inet 192.168.86.234/24 brd 192.168.86.255 scope global dynamic noprefixroute <wireless card name>
- ```
- In this case my local IP was 192.168.86.234
-
-Once you have identified your local IP address, you can access the website running on your computer by visiting `http://<local IP>:8443` replacing `<local IP>` (including the brackets) with the value you found using the instructions above in your mobile devices browser.
+You can also access it from other devices in your same network (like a mobile phone) for additional testing. See the [finding your local IP address](./README.md#finding-your-local-ip-address) section of the README for how to identify the local IP adress Nuxt is served on. Once you have identified your local IP address, you can access the website running on your computer by visiting `https://<local IP>:8443` replacing `<local IP>` (including the brackets) with the value you found using the instructions above in your mobile devices browser.
Testing from multiple different devices as often as possible is a great way to contribute to Openverse's frontend development.
diff --git a/nuxt.config.ts b/nuxt.config.ts
index 762864f15..727775362 100644
--- a/nuxt.config.ts
+++ b/nuxt.config.ts
@@ -1,3 +1,6 @@
+import path from 'path'
+import fs from 'fs'
+
import pkg from './package.json'
import locales from './src/locales/scripts/valid-locales.json'
@@ -117,7 +120,15 @@ const config: NuxtConfig = {
},
srcDir: 'src/',
modern: 'client',
- server: { port: process.env.PORT || 8443 },
+ server: {
+ port: process.env.PORT || 8443,
+ https: process.env.LOCAL_SSL
+ ? {
+ key: fs.readFileSync(path.resolve(__dirname, 'localhost+1-key.pem')),
+ cert: fs.readFileSync(path.resolve(__dirname, 'localhost+1.pem')),
+ }
+ : undefined,
+ },
router: {
middleware: 'middleware',
},
diff --git a/package.json b/package.json
index 66e225cf5..94ac0af19 100644
--- a/package.json
+++ b/package.json
@@ -13,6 +13,7 @@
"private": true,
"scripts": {
"dev": "pnpm install && pnpm i18n:create-locales-list && pnpm i18n:update-locales && cross-env nuxt --hostname 0.0.0.0",
+ "dev:secure": "LOCAL_SSL=enabled pnpm dev",
"build": "pnpm i18n:get-translations && nuxt build",
"build-only": "nuxt build",
"generate": "nuxt generate",