Skip to content

Commit 1ded727

Browse files
committed
docs
1 parent 0af6afb commit 1ded727

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

packages/docs/src/routes/api/qwik/api.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1774,7 +1774,7 @@
17741774
}
17751775
],
17761776
"kind": "Function",
1777-
"content": "> This API is provided as an alpha preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.\n> \n\n> Warning: This API is now obsolete.\n> \n> This is no longer needed as the preloading happens automatically in qrl-class.ts. Leave this in your app for a while so it uninstalls existing service workers, but don't use it for new projects.\n> \n\n\n```typescript\nPrefetchServiceWorker: (opts: {\n base?: string;\n scope?: string;\n path?: string;\n verbose?: boolean;\n fetchBundleGraph?: boolean;\n nonce?: string;\n}) => JSXNode<'script'>\n```\n\n\n<table><thead><tr><th>\n\nParameter\n\n\n</th><th>\n\nType\n\n\n</th><th>\n\nDescription\n\n\n</th></tr></thead>\n<tbody><tr><td>\n\nopts\n\n\n</td><td>\n\n{ base?: string; scope?: string; path?: string; verbose?: boolean; fetchBundleGraph?: boolean; nonce?: string; }\n\n\n</td><td>\n\n\n</td></tr>\n</tbody></table>\n**Returns:**\n\n[JSXNode](#jsxnode)<!-- -->&lt;'script'&gt;",
1777+
"content": "> This API is provided as an alpha preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.\n> \n\n> Warning: This API is now obsolete.\n> \n> This is no longer needed as the preloading happens automatically in qrl-class.ts. Leave this in your app for a while so it uninstalls existing service workers, but don't use it for new projects.\n> \n\n\n```typescript\nPrefetchServiceWorker: (opts: {\n base?: string;\n scope?: string;\n path?: string;\n verbose?: boolean;\n fetchBundleGraph?: boolean;\n nonce?: string;\n}) => JSXNode<'script'>\n```\n\n\n<table><thead><tr><th>\n\nParameter\n\n\n</th><th>\n\nType\n\n\n</th><th>\n\nDescription\n\n\n</th></tr></thead>\n<tbody><tr><td>\n\nopts\n\n\n</td><td>\n\n{ base?: string; scope?: string; path?: string; verbose?: boolean; fetchBundleGraph?: boolean; nonce?: string; }\n\n\n</td><td>\n\n\n</td></tr>\n</tbody></table>\n**Returns:**\n\nJSXNode&lt;'script'&gt;",
17781778
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik/src/core/components/prefetch.ts",
17791779
"mdFile": "qwik.prefetchserviceworker.md"
17801780
},

packages/docs/src/routes/api/qwik/index.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3651,7 +3651,7 @@ opts
36513651
</tbody></table>
36523652
**Returns:**
36533653
3654-
[JSXNode](#jsxnode)&lt;'script'&gt;
3654+
JSXNode&lt;'script'&gt;
36553655
36563656
[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik/src/core/components/prefetch.ts)
36573657
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
title: Rewrites | Guides
3+
description: Learn how to use rewrites in Qwik City.
4+
contributors:
5+
- omerman
6+
updated_at: '2025-05-04T19:43:33Z'
7+
created_at: '2025-05-04T23:45:13Z'
8+
---
9+
10+
# Rewrites
11+
12+
Sometimes you want to redirect a user from the current page to another page,
13+
but you want to keep the current URL in the browser history.
14+
15+
Let's say a user tries to access an article which is indexed by its name,
16+
e.g `/articles/qwik-is-very-quick`.
17+
but in our code, we access it by its id, on our directory structure.
18+
19+
```
20+
src/routes/articles/
21+
├── [id]
22+
├─── index.tsx
23+
```
24+
25+
26+
```tsx title="src/routes/[email protected]"
27+
import type { RequestHandler } from "@builder.io/qwik-city";
28+
29+
export const onRequest: RequestHandler = async ({ url, rewrite }) => {
30+
const pattern = /^\/articles\/(.*)$/;
31+
// Detects /articles/<article-name>, returns null if url does not match the pattern.
32+
const match = url.pathname.match(pattern);
33+
if (match) {
34+
const articleName = match[1];
35+
const { id } = await db.getArticleByName(articleName);
36+
throw rewrite(`/articles/${id}`);
37+
}
38+
};
39+
```
40+
41+
The `rewrite()` function, which was destructured in the RequestHandler function arguments, takes a pathname string or a URL object.
42+
43+
```tsx
44+
throw rewrite(`/articles/777/`);
45+
throw rewrite(new URL(`/articles/777/`, url));
46+
```
47+
48+
Note: If you provide a relative path, Qwik City will rewrite the URL and apply search params from the original URL.

0 commit comments

Comments
 (0)