URL
The URL API can be helpful in instances where you need to parse your application’s URL. SvelteKit makes accessing the URL API quite simple, as it is also available in the event
object, just as the request
object was. Let’s continue with our previous example using comments. If we wanted to build a commenting service for use across a network of other websites, we may want to report to a user which site they just commented on. Let’s expand on that previous example and do just that. We don’t need to make any changes to src/routes/comment/+page.svelte
. Instead, we’ll adjust our server action accordingly.
This example is identical to the last except for a couple of changes. The URL API is accessed via event.url
and assigned to the url
constant. It is then output to the server with console.log(url)
to show you the various read-only properties that you can access. For this demonstration, we get the hostname via url.hostname
and use it in a template...