-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow dynamic routing based on file paths (#55)
* feat: allow dynamic routing based on file paths * docs: add dynamic routes documentation * feat: fix collision between static assets and dynamic routes. Update examples and docs * fix: update documentation to highlight static assets have lower priority than fixed routes Co-authored-by: Rafael Fernández López <[email protected]>
- Loading branch information
1 parent
be99d39
commit f96d544
Showing
32 changed files
with
920 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
target | ||
*.wasm | ||
!tests/**/*.wasm | ||
examples/*.toml | ||
.DS_Store |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
sidebar_position: 3 | ||
--- | ||
|
||
# Dynamic routes | ||
|
||
Defining static routes may not be enough for some applications. You may need a worker to process URLs that includes identifiers. **To create a worker associated with a dynamic route, include the route parameter in brackets when setting the worker filename**. | ||
|
||
For example, imagine you want a worker that replies to the following URLs: | ||
|
||
- `/show/1` | ||
- `/show/2` | ||
|
||
With dynamic routes, you can create a worker with the `show/[id].js` filename. This worker will reply to any `/show/X` route automatically. | ||
|
||
After defining the route paremeters, the worker receives a special argument called `params`. It includes the value of all defined parameters. Note that the name of the parameter will be defined by the text between the brackets. | ||
|
||
Check these guides to understand how to read parameters in the different supported languages: | ||
|
||
* [Dynamic routes in JavaScript](../tutorials/javascript-workers.md#dynamic-routes) | ||
* [Dynamic routes in Rust](../tutorials/rust-workers.md#dynamic-routes) | ||
|
||
## Dynamic routes and folders | ||
|
||
Folders can follow this pattern too. You can define a folder that has a route parameter: | ||
|
||
``` | ||
$ tree ./examples/with-params | ||
./examples/with-params | ||
├── [id] | ||
└── fixed.js | ||
``` | ||
|
||
In this case, the `./[id]/fixed.js` worker can reply to URLs like `/abc/fixed` and `/test/fixed`. | ||
|
||
## Multiple parameters | ||
|
||
As both files and folders can be dynamic, workers may receive multiple parameters. The `params` object includes the value of all the different parameters. | ||
|
||
``` | ||
$ tree . | ||
. | ||
├── [resource] | ||
└── [id] | ||
└── show.js | ||
``` | ||
|
||
In this case, the `./[resource]/[id]/show.js` worker replies to URLs like `/articles/2/show`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 3 | ||
sidebar_position: 4 | ||
--- | ||
|
||
# Environment Variables | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Builds a reply to the given request | ||
*/ | ||
const reply = (req) => { | ||
// Body response | ||
const body = `<!DOCTYPE html> | ||
<head> | ||
<title>Wasm Workers Server</title> | ||
<meta name="viewport" content="width=device-width,initial-scale=1"> | ||
<meta charset="UTF-8"> | ||
<link rel="stylesheet" href="/water.min.css"> | ||
<link rel="stylesheet" href="/main.css"> | ||
</head> | ||
<body> | ||
<main> | ||
<h1>Hello from Wasm Workers Server 👋</h1> | ||
<p> | ||
This is a dynamic route! The <code>[id].js</code> worker is replying this URL. | ||
The <code>id</code> parameter value is: <code>${req.params?.id}</code> | ||
</p> | ||
<p>Read more about dynamic routes <a href="https://workers.wasmlabs.dev/docs/features/dynamic-routes">in the documentation</a></p> | ||
</main> | ||
</body>`; | ||
|
||
return new Response(body); | ||
} | ||
|
||
// Subscribe to the Fetch event | ||
addEventListener("fetch", event => { | ||
return event.respondWith(reply(event.request)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Builds a reply to the given request | ||
*/ | ||
const reply = (req) => { | ||
// Body response | ||
const body = `<!DOCTYPE html> | ||
<head> | ||
<title>Wasm Workers Server</title> | ||
<meta name="viewport" content="width=device-width,initial-scale=1"> | ||
<meta charset="UTF-8"> | ||
<link rel="stylesheet" href="/water.min.css"> | ||
<link rel="stylesheet" href="/main.css"> | ||
</head> | ||
<body> | ||
<main> | ||
<h1>Hello from Wasm Workers Server 👋</h1> | ||
<p> | ||
This is a dynamic route! The <code>[id]/fixed.js</code> worker is replying this URL. | ||
The <code>id</code> parameter value is: <code>${req.params?.id}</code> | ||
</p> | ||
<p>Read more about dynamic routes <a href="https://workers.wasmlabs.dev/docs/features/dynamic-routes">in the documentation</a></p> | ||
</main> | ||
</body>`; | ||
|
||
return new Response(body); | ||
} | ||
|
||
// Subscribe to the Fetch event | ||
addEventListener("fetch", event => { | ||
return event.respondWith(reply(event.request)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Builds a reply to the given request | ||
*/ | ||
const reply = () => { | ||
// Body response | ||
const body = `<!DOCTYPE html> | ||
<head> | ||
<title>Wasm Workers Server</title> | ||
<meta name="viewport" content="width=device-width,initial-scale=1"> | ||
<meta charset="UTF-8"> | ||
<link rel="stylesheet" href="/water.min.css"> | ||
<link rel="stylesheet" href="/main.css"> | ||
</head> | ||
<body> | ||
<main> | ||
<h1>Hello from Wasm Workers Server 👋</h1> | ||
<p> | ||
This is a fixed route. There isn't any parameter here. | ||
</p> | ||
<p>Read more about dynamic routes <a href="https://workers.wasmlabs.dev/docs/features/dynamic-routes">in the documentation</a></p> | ||
</main> | ||
</body>`; | ||
|
||
return new Response(body); | ||
} | ||
|
||
// Subscribe to the Fetch event | ||
addEventListener("fetch", event => { | ||
return event.respondWith(reply(event.request)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
body { | ||
max-width: 1000px; | ||
} | ||
|
||
main { | ||
margin: 5rem 0; | ||
} | ||
|
||
h1, | ||
p { | ||
text-align: center; | ||
} | ||
|
||
h1 { | ||
margin-bottom: 2rem; | ||
} | ||
|
||
pre { | ||
font-size: .9rem; | ||
} | ||
|
||
pre>code { | ||
padding: 2rem; | ||
} | ||
|
||
p { | ||
margin-top: 2rem; | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Builds a reply to the given request | ||
*/ | ||
const reply = (req) => { | ||
// Body response | ||
const body = `<!DOCTYPE html> | ||
<head> | ||
<title>Wasm Workers Server</title> | ||
<meta name="viewport" content="width=device-width,initial-scale=1"> | ||
<meta charset="UTF-8"> | ||
<link rel="stylesheet" href="/water.min.css"> | ||
<link rel="stylesheet" href="/main.css"> | ||
</head> | ||
<body> | ||
<main> | ||
<h1>Hello from Wasm Workers Server 👋</h1> | ||
<p> | ||
This is a dynamic route! The <code>sub/[id].js</code> worker is replying this URL. | ||
The <code>id</code> parameter value is: <code>${req.params?.id}</code> | ||
</p> | ||
<p>Read more about dynamic routes <a href="https://workers.wasmlabs.dev/docs/features/dynamic-routes">in the documentation</a></p> | ||
</main> | ||
</body>`; | ||
|
||
return new Response(body); | ||
} | ||
|
||
// Subscribe to the Fetch event | ||
addEventListener("fetch", event => { | ||
return event.respondWith(reply(event.request)); | ||
}); |
Oops, something went wrong.