Common issues and their solutions when working with Swiftlets. Get unstuck and back to building amazing web apps.
Swift compiler gets stuck on complex HTML expressions, causing builds to hang indefinitely or timeout after 30 seconds.
Generic parameter inference failures when using conditionals or complex expressions in HTML builders.
Incorrect syntax with @Query, @FormValue, @Cookie, or other property wrappers causing compilation errors.
Using outdated method names like .attr() instead of .attribute(), or missing imports.
The most common issue is Swift's type checker struggling with complex HTML structures. Here's how to fix it:
⚠️ When the build script times out after 30 seconds, it means your HTML structure is too complex for Swift's type checker. The solution is function decomposition.
Body {
Nav {
Container {
HStack {
Link(href: "/") { H1("Title") }
Spacer()
HStack(spacing: 20) {
Link(href: "/docs", "Docs")
Link(href: "/about", "About")
// Many more links...
}
}
}
}
Container {
VStack(spacing: 40) {
Section {
H2("Overview")
P("Long content...")
Grid(columns: .count(2)) {
// Many grid items...
}
}
// Many more sections...
}
}
Footer {
// Complex footer...
}
}
@main
struct MyPage: SwiftletMain {
var title = "My Page"
var body: some HTMLElement {
Fragment {
navigation()
mainContent()
footer()
}
}
@HTMLBuilder
func navigation() -> some HTMLElement {
Nav {
Container {
HStack {
Link(href: "/") { H1("Title") }
Spacer()
navigationLinks()
}
}
}
}
@HTMLBuilder
func navigationLinks() -> some HTMLElement {
HStack(spacing: 20) {
Link(href: "/docs", "Docs")
Link(href: "/about", "About")
}
}
@HTMLBuilder
func mainContent() -> some HTMLElement {
Container {
VStack(spacing: 40) {
overviewSection()
detailsSection()
}
}
}
}
Cause: Using 'if' statements in HTMLBuilder contexts
if showButton {
Button("Click me")
}
If(showButton) {
Button("Click me")
}
Cause: Using outdated method names
Link(href: "/example", "Link")
.attr("target", "_blank")
Link(href: "/example", "Link")
.attribute("target", "_blank")
Cause: Incorrect property wrapper syntax
@Cookie var theme: String?
@Cookie("theme", default: "light") var theme: String?
Use @HTMLBuilder functions returning 'some HTMLElement' for logical sections
Replace 'if' statements with 'If()' helper in HTMLBuilder contexts
Use .attribute() not .attr(), ensure you have the latest API
Include parameter names and defaults for @Cookie, @Query, etc.
Custom HTMLComponent instances need .body property or helper functions
Place reusable components in src/shared/ directory
This usually happens when Swift's type checker encounters overly complex HTML expressions. Break your HTML into smaller @HTMLBuilder functions that return 'some HTMLElement'.
Use 'some HTMLElement' for @HTMLBuilder functions. 'some HTML' is for complete page structures. The type system expects HTMLElement in most contexts.
The build script will show which file is hanging. Look for deeply nested HTML, many chained modifiers, or complex expressions. Start by extracting the largest sections into separate functions.
No, use the 'If()' helper instead. Regular 'if' statements cause generic parameter inference issues in result builder contexts.
Swift's result builders don't allow declarations. Move your struct definitions outside the HTML builder closure or to separate files.
Consider decomposition when you have: 3-4+ levels of nesting, sections over 50-60 lines, multiple complex modifiers, or compilation taking more than a few seconds.
If these solutions don't help, here are additional resources: