33
loading...
This website collects cookies to deliver better user experience
POST /links
endpoint, and will be served up using Workers Sites.npm i @cloudflare/kv-asset-handler
Move the index.js
, package.json
and package-lock.json
from the project root into their own folder, which we'll call workers-site
.
Create a public
directory in your project root, with a static
subdirectory in it.
Modify your wrangler.toml
file to add this section at the bottom:
[site]
bucket = "./public"
entry-point = "workers-site"
public
to their own KV namespace.redirect
):redirect
| wrangler.toml
└───public
└───static
└───workers-site
└───index.js
└───package.json
└───package-lock.json
public/static
directory. index.html
file from the finished project directly into the public
folder. This tutorial won't focus on the specifics of Vue too much, but let's explore what the code is doing. Looking at this section of the code (line 16-32):<form @submit.prevent="handleUrlSubmit">
<input
type="text"
id="input-url"
name="url"
size="40"
placeholder="https://google.com"
required
v-model="longUrl"
/>
<input type="submit" id="input-submit" value="Shorten" />
</form>
<div class="message" v-if="shortUrl">
<p>Your new shortened URL is:</p>
<h2>{{ shortUrl }}</h2>
</div>
v-model
directive. Whenever the input box for the URL is updated, the longUrl
data property will be updated.submit
event on this form. The handleUrlSubmit
method we define will take care of interacting with the endpoint we defined before, and will update the shortUrl
data property. Once this is available, the URL will be displayed to the user (visibility toggled by the v-if
directive).handleUrlSubmit
method on the Vue app:methods: {
handleUrlSubmit() {
fetch('/links', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
url: this.longUrl,
}),
})
.then((response) => {
if (response.status == 200) {
this.error = false;
return response.json();
} else {
throw new Error('Issue saving URL');
}
})
.then((data) => {
this.shortUrl = data.shortened;
})
.catch((error) => {
this.error = true;
});
},
}
fetch
request (with very little error handling) to our /links
endpoint, and retrieving the shortened
value from the API response. The shortUrl
data property gets set to this value, and the shortened URL is displayed to the user./
)/static/style.css
)kv-asset-handler
's getAssetFromKV
function, which consumes a FetchEvent
and returns a Response
based on what's stored in the KV namespace for static assets.index.js
. First, import the getAssetFromKV
function:import { getAssetFromKV } from '@cloudflare/kv-asset-handler';
async function handleEvent(event) {
let requestUrl = new URL(event.request.url);
if (requestUrl.pathname === '/' || requestUrl.pathname.includes('static')) {
return await getAssetFromKV(event);
} else {
return await router.handle(event.request);
}
}
Request
object, while the getAssetFromKV
function takes in the whole FetchEvent
. Next, let's hook into this function on our fetch event listener. Modify the listener from:addEventListener('fetch', event => {
event.respondWith(router.handle(event.request))
})
addEventListener('fetch', event => {
event.respondWith(handleEvent(event));
});
wrangler dev
. Notice you'll get a bit of extra output as your static assets get populated into their own KV namespace:$ wrangler dev
🌀 Using namespace for Workers Site "__redirect-workers_sites_assets_preview"
✨ Success
👂 Listening on http://127.0.0.1:8787
127.0.0.1:8787/nFXAau
for me), it should redirect! Our routing has been set up properly.