Improved the static assets definitions in the Website service (#18)
This PR contains the work done to overhaul the static asset definitions for the Website service.
To provide further details about the work:
* Website library
* Overhauled the `StateFile` enumeration to reduce the number of cases to one case per logical file name, each exposing a `fileExtensions` list.
* Wired everything into the pages with a consistent ordering convention: stylesheets load shared-first so the page sheet wins the CSS cascade; scripts load page-first with shared.js last. The error page also gained the shared stylesheet and its scripts; the index page gained its page CSS/JS and the new touch icon link.
* Used the `StaticFile` enumeration as a single source of truth for every _href_/_src_ in the `IndexPage` and the `ErrorPage` pages, eliminating hardcoded asset paths.
* Website service
* Added new assets to the Resources folder:
* `apple-touch-icon.png`
* `css/index.css`
* `js/index.js`
* `js/error.js`
* `sitemap.xml`
* Renamed existing assets within the Resources folder:
* `css/style.css` → `css/shared.css`
* `js/app.js` → `js/shared.js`
* Fixed the working-directory location for the scheme in the Xcode project.
Reviewed-on: rock-n-code/loud-amsterdam#18
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit is contained in:
@@ -94,20 +94,22 @@ struct AppTests {
|
||||
try await app(
|
||||
staticFilesPath: staticFilesPath
|
||||
).test(.router) { client in
|
||||
try await client.execute(
|
||||
uri: "/\(file.relativePath)",
|
||||
method: .get
|
||||
) { response in
|
||||
#expect(response.status == .ok)
|
||||
#expect(response.headers[.contentType] == file.contentType)
|
||||
|
||||
let cacheControl = try #require(response.headers[.cacheControl])
|
||||
for fileExtension in file.fileExtensions {
|
||||
try await client.execute(
|
||||
uri: "/\(file.relativePath(for: fileExtension))",
|
||||
method: .get
|
||||
) { response in
|
||||
#expect(response.status == .ok)
|
||||
#expect(response.headers[.contentType] == fileExtension.contentType)
|
||||
|
||||
#expect(cacheControl.contains("public") == true)
|
||||
#expect(cacheControl.contains("max-age=") == true)
|
||||
|
||||
if textExtensions.contains(file.fileExtension) {
|
||||
#expect(cacheControl.contains("must-revalidate") == true)
|
||||
let cacheControl = try #require(response.headers[.cacheControl])
|
||||
|
||||
#expect(cacheControl.contains("public") == true)
|
||||
#expect(cacheControl.contains("max-age=") == true)
|
||||
|
||||
if textExtensions.contains(fileExtension) {
|
||||
#expect(cacheControl.contains("must-revalidate") == true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,36 +5,25 @@ import Testing
|
||||
|
||||
@Suite("StaticFile enumeration")
|
||||
struct StaticFileTests {
|
||||
|
||||
|
||||
// MARK: Type aliases
|
||||
|
||||
|
||||
typealias File = StaticFile
|
||||
typealias FileExtension = StaticFile.Extension
|
||||
|
||||
|
||||
// MARK: Computed tests
|
||||
|
||||
@Test(arguments: zip(
|
||||
File.allCases,
|
||||
Self.contentTypes
|
||||
))
|
||||
func `content type`(
|
||||
for file: File,
|
||||
expects contentType: String
|
||||
) {
|
||||
#expect(file.contentType == contentType)
|
||||
}
|
||||
|
||||
|
||||
@Test(arguments: zip(
|
||||
File.allCases,
|
||||
Self.fileExtensions
|
||||
))
|
||||
func `file extension`(
|
||||
func `file extensions`(
|
||||
for file: File,
|
||||
expects `extension`: FileExtension
|
||||
expects extensions: [FileExtension]
|
||||
) {
|
||||
#expect(file.fileExtension == `extension`)
|
||||
#expect(file.fileExtensions == extensions)
|
||||
}
|
||||
|
||||
|
||||
@Test(arguments: zip(
|
||||
File.allCases,
|
||||
Self.fileNames
|
||||
@@ -45,20 +34,57 @@ struct StaticFileTests {
|
||||
) {
|
||||
#expect(file.fileName == fileName)
|
||||
}
|
||||
|
||||
|
||||
@Test(arguments: zip(
|
||||
Self.extensions,
|
||||
Self.contentTypes
|
||||
))
|
||||
func `content type`(
|
||||
for fileExtension: FileExtension,
|
||||
expects contentType: String
|
||||
) {
|
||||
#expect(fileExtension.contentType == contentType)
|
||||
}
|
||||
|
||||
@Test(arguments: zip(
|
||||
Self.extensions,
|
||||
Self.subdirectories
|
||||
))
|
||||
func `subdirectory`(
|
||||
for fileExtension: FileExtension,
|
||||
expects subdirectory: String?
|
||||
) {
|
||||
#expect(fileExtension.subdirectory == subdirectory)
|
||||
}
|
||||
|
||||
// MARK: Method tests
|
||||
|
||||
@Test(arguments: zip(
|
||||
File.allCases,
|
||||
Self.relativePaths
|
||||
))
|
||||
func `relative path`(
|
||||
func `relative path for`(
|
||||
for file: File,
|
||||
expects relativePath: String
|
||||
expects relativePaths: [String]
|
||||
) {
|
||||
#expect(file.relativePath == relativePath)
|
||||
for (fileExtension, relativePath) in zip(file.fileExtensions, relativePaths) {
|
||||
#expect(file.relativePath(for: fileExtension) == relativePath)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Method tests
|
||||
|
||||
|
||||
@Test(arguments: zip(
|
||||
File.allCases,
|
||||
Self.relativePaths
|
||||
))
|
||||
func `url path for`(
|
||||
for file: File,
|
||||
expects relativePaths: [String]
|
||||
) {
|
||||
for (fileExtension, relativePath) in zip(file.fileExtensions, relativePaths) {
|
||||
#expect(file.urlPath(for: fileExtension) == "/\(relativePath)")
|
||||
}
|
||||
}
|
||||
|
||||
@Test(arguments: [
|
||||
"",
|
||||
".",
|
||||
@@ -68,70 +94,99 @@ struct StaticFileTests {
|
||||
_ basePath: String
|
||||
) {
|
||||
for file in File.allCases {
|
||||
let pathRelativeToBasePath = file.path(relativeTo: basePath)
|
||||
for fileExtension in file.fileExtensions {
|
||||
let pathRelativeToBasePath = file.path(
|
||||
relativeTo: basePath,
|
||||
for: fileExtension
|
||||
)
|
||||
let relativePath = file.relativePath(for: fileExtension)
|
||||
|
||||
if basePath.isEmpty {
|
||||
#expect(pathRelativeToBasePath == file.relativePath)
|
||||
} else {
|
||||
#expect(pathRelativeToBasePath == "\(basePath)/\(file.relativePath)")
|
||||
if basePath.isEmpty {
|
||||
#expect(pathRelativeToBasePath == relativePath)
|
||||
} else {
|
||||
#expect(pathRelativeToBasePath == "\(basePath)/\(relativePath)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: CaseIterable tests
|
||||
|
||||
|
||||
@Test
|
||||
func `all cases`() {
|
||||
#expect(File.allCases.count == 8)
|
||||
#expect(File.allCases.count == 9)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private extension StaticFileTests {
|
||||
|
||||
|
||||
// MARK: Constants
|
||||
|
||||
static let contentTypes: [String] = [
|
||||
"text/javascript",
|
||||
"text/css",
|
||||
"image/vnd.microsoft.icon",
|
||||
"image/png",
|
||||
"image/svg+xml",
|
||||
"text/plain",
|
||||
"application/manifest+json",
|
||||
"text/css"
|
||||
]
|
||||
static let fileExtensions: [FileExtension] = [
|
||||
.js,
|
||||
|
||||
static let extensions: [FileExtension] = [
|
||||
.css,
|
||||
.ico,
|
||||
.js,
|
||||
.png,
|
||||
.ico,
|
||||
.svg,
|
||||
.txt,
|
||||
.webmanifest,
|
||||
.css
|
||||
.xml
|
||||
]
|
||||
static let contentTypes: [String] = [
|
||||
"text/css",
|
||||
"text/javascript",
|
||||
"image/png",
|
||||
"image/vnd.microsoft.icon",
|
||||
"image/svg+xml",
|
||||
"text/plain",
|
||||
"application/manifest+json",
|
||||
"application/xml"
|
||||
]
|
||||
static let subdirectories: [String?] = [
|
||||
"css",
|
||||
"js",
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
nil
|
||||
]
|
||||
static let fileExtensions: [[FileExtension]] = [
|
||||
[.png],
|
||||
[.css, .js],
|
||||
[.ico],
|
||||
[.png, .svg],
|
||||
[.css, .js],
|
||||
[.txt],
|
||||
[.css, .js],
|
||||
[.webmanifest],
|
||||
[.xml]
|
||||
]
|
||||
static let fileNames: [String] = [
|
||||
"app",
|
||||
"apple-touch-icon",
|
||||
"error",
|
||||
"favicon",
|
||||
"icon",
|
||||
"icon",
|
||||
"index",
|
||||
"robots",
|
||||
"shared",
|
||||
"site",
|
||||
"style"
|
||||
"sitemap"
|
||||
]
|
||||
static let relativePaths: [String] = [
|
||||
"js/app.js",
|
||||
"css/error.css",
|
||||
"favicon.ico",
|
||||
"icon.png",
|
||||
"icon.svg",
|
||||
"robots.txt",
|
||||
"site.webmanifest",
|
||||
"css/style.css"
|
||||
static let relativePaths: [[String]] = [
|
||||
["apple-touch-icon.png"],
|
||||
["css/error.css", "js/error.js"],
|
||||
["favicon.ico"],
|
||||
["icon.png", "icon.svg"],
|
||||
["css/index.css", "js/index.js"],
|
||||
["robots.txt"],
|
||||
["css/shared.css", "js/shared.js"],
|
||||
["site.webmanifest"],
|
||||
["sitemap.xml"]
|
||||
]
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -19,7 +19,10 @@ struct ErrorPageTests {
|
||||
#expect(html.contains(#"lang="en""#))
|
||||
#expect(html.contains("Page Not Found"))
|
||||
#expect(html.contains("Sorry, but the page you were trying to view does not exist."))
|
||||
#expect(html.contains("/css/shared.css"))
|
||||
#expect(html.contains("/css/error.css"))
|
||||
#expect(html.contains("/js/error.js"))
|
||||
#expect(html.contains("/js/shared.js"))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,13 +17,15 @@ struct IndexPageTests {
|
||||
|
||||
#expect(html.contains("<!DOCTYPE html>"))
|
||||
#expect(html.contains(#"lang="en""#))
|
||||
#expect(html.contains("/css/style.css"))
|
||||
#expect(html.contains("/css/shared.css"))
|
||||
#expect(html.contains("/css/index.css"))
|
||||
#expect(html.contains("/favicon.ico"))
|
||||
#expect(html.contains("/icon.svg"))
|
||||
#expect(html.contains("/icon.png"))
|
||||
#expect(html.contains("/apple-touch-icon.png"))
|
||||
#expect(html.contains("/site.webmanifest"))
|
||||
#expect(html.contains("Hello world!"))
|
||||
#expect(html.contains("/js/app.js"))
|
||||
#expect(html.contains("/js/shared.js"))
|
||||
#expect(html.contains("/js/index.js"))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user