0
# Utility Functions
1
2
Helper functions used internally by the adapter for route processing and pattern conversion. These functions are part of the adapter's internal implementation and are not exposed in the public API.
3
4
## Capabilities
5
6
### Route Pathname Generation
7
8
Generates pathname strings from SvelteKit route definitions for ISR (Incremental Static Regeneration) configuration.
9
10
```typescript { .api }
11
/**
12
* Generates pathname from route segments for ISR configuration
13
* Converts dynamic route segments into numbered placeholders ($1, $2, etc.)
14
* @param route - SvelteKit route definition with segment information
15
* @returns Pathname string with parameter placeholders
16
*/
17
function get_pathname(route: RouteDefinition<any>): string;
18
19
// Supporting types from @sveltejs/kit
20
interface RouteDefinition<Config = any> {
21
id: string;
22
pattern: RegExp;
23
segments: Array<{
24
content: string;
25
dynamic: boolean;
26
rest: boolean;
27
}>;
28
methods: string[];
29
prerender?: boolean | 'auto';
30
config?: Config;
31
}
32
```
33
34
**Internal Usage Examples:**
35
36
```javascript
37
// Example of internal usage within the adapter
38
// These are not available for external import
39
40
// For route: /blog/[slug]
41
const route = {
42
segments: [
43
{ content: 'blog', dynamic: false, rest: false },
44
{ content: '[slug]', dynamic: true, rest: false }
45
]
46
};
47
const pathname = get_pathname(route);
48
// Result: "blog/$1"
49
50
// For route: /api/[...rest]
51
const restRoute = {
52
segments: [
53
{ content: 'api', dynamic: false, rest: false },
54
{ content: '[...rest]', dynamic: true, rest: true }
55
]
56
};
57
const restPathname = get_pathname(restRoute);
58
// Result: "api$1"
59
60
// For route with optional parameters: /shop/[[category]]/[id]
61
const optionalRoute = {
62
segments: [
63
{ content: 'shop', dynamic: false, rest: false },
64
{ content: '[[category]]', dynamic: true, rest: false },
65
{ content: '[id]', dynamic: true, rest: false }
66
]
67
};
68
const optionalPathname = get_pathname(optionalRoute);
69
// Result: "shop/$1/$2"
70
```
71
72
### Route Pattern Conversion
73
74
Converts SvelteKit route regex patterns to Vercel routing source format for deployment configuration.
75
76
```typescript { .api }
77
/**
78
* Adjusts stringified route regex for Vercel's routing system
79
* Converts SvelteKit route patterns to Vercel-compatible source patterns
80
* @param pattern - Stringified route regex from SvelteKit
81
* @returns Source pattern compatible with Vercel routing
82
*/
83
function pattern_to_src(pattern: string): string;
84
```
85
86
**Internal Usage Examples:**
87
88
```javascript
89
// Example of internal usage within the adapter
90
// These are not available for external import
91
92
// Root route conversion
93
const rootPattern = "/^/$/";
94
const rootSrc = pattern_to_src(rootPattern);
95
// Result: "^/?"
96
97
// Parameter route conversion
98
const paramPattern = "/^/foo/([^/]+?)/?$/";
99
const paramSrc = pattern_to_src(paramPattern);
100
// Result: "^/foo/([^/]+?)/?$"
101
102
// Optional parameter conversion
103
const optionalPattern = "/^/foo(/[^/]+)?/?$/";
104
const optionalSrc = pattern_to_src(optionalPattern);
105
// Result: "^/foo(/[^/]+)?/?$"
106
107
// Rest parameter conversion
108
const restPattern = "/^/foo(/[^]*)?/?$/";
109
const restSrc = pattern_to_src(restPattern);
110
// Result: "^/foo(/[^]*)?/?$"
111
```
112
113
## Internal Processing Details
114
115
These utilities handle special cases in route processing:
116
117
### Dynamic Segment Processing
118
119
The `get_pathname` function processes different types of dynamic segments:
120
121
- **Required parameters** `[param]`: Converted to `$1`, `$2`, etc.
122
- **Optional parameters** `[[param]]`: Converted to `$1`, `$2`, etc.
123
- **Rest parameters** `[...rest]`: Converted to `$1`, `$2`, etc.
124
- **Mixed segments** `prefix-[param]-suffix`: Converted to `prefix-$1-suffix`
125
126
### Special Handling for Rest Parameters
127
128
When a segment contains only a single optional or rest parameter, no leading slash is prepended to avoid incorrect trailing slash redirects in ISR scenarios.
129
130
```javascript
131
// Single rest parameter segment
132
{ content: '[...rest]', dynamic: true, rest: true }
133
// Results in: "$1" (no leading slash)
134
135
// Rest parameter in mixed content
136
{ content: 'api-[...rest]', dynamic: true, rest: true }
137
// Results in: "/api-$1" (with leading slash)
138
```
139
140
### Pattern Optimization for Vercel
141
142
The `pattern_to_src` function optimizes patterns for Vercel's routing:
143
144
- Removes regex delimiters and flags
145
- Handles root route special case (`^/` becomes `^/?`)
146
- Moves non-capturing groups into following capturing groups for ISR compatibility
147
- Prevents false trailing slash redirects in edge cases
148
149
```javascript
150
// Non-capturing group optimization
151
// Input: "(?:/([^/]+))"
152
// Output: "(/$1)"
153
// This prevents empty rest parameters from creating unwanted trailing slashes
154
```