-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2700 from SwiftPackageIndex/404-sitemaps
- Loading branch information
Showing
7 changed files
with
176 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright Dave Verwer, Sven A. Schmidt, and other contributors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import Foundation | ||
import Plot | ||
|
||
enum SiteMapView { | ||
|
||
static var staticRoutes: [SiteURL] = [ | ||
.home, | ||
.addAPackage, | ||
.faq, | ||
.supporters, | ||
.buildMonitor, | ||
.privacy | ||
] | ||
|
||
static func index(packages: [SiteMapController.Package]) -> SiteMapIndex { | ||
SiteMapIndex( | ||
.sitemap( | ||
.loc(SiteURL.siteMapStaticPages.absoluteURL()), | ||
.lastmod(Current.date(), timeZone: .utc) // The home page updates every day. | ||
), | ||
.group( | ||
packages.map { package -> Node<SiteMapIndex.SiteMapIndexContext> in | ||
.sitemap( | ||
.loc(SiteURL.package(.value(package.owner), | ||
.value(package.repository), | ||
.siteMap).absoluteURL()), | ||
.unwrap(package.lastActivityAt, { .lastmod($0, timeZone: .utc) }) | ||
) | ||
} | ||
) | ||
) | ||
} | ||
|
||
static func staticPages() -> SiteMap { | ||
SiteMap( | ||
.group( | ||
staticRoutes.map { page -> Node<SiteMap.URLSetContext> in | ||
.url( | ||
.loc(page.absoluteURL()) | ||
) | ||
} | ||
) | ||
) | ||
} | ||
|
||
static func package(owner: String?, | ||
repository: String?, | ||
lastActivityAt: Date?, | ||
linkablePathUrls: [String]) async throws -> SiteMap { | ||
guard let owner, | ||
let repository | ||
else { | ||
// This should never happen, but we should return an empty | ||
// sitemap instead of an incorrect one. | ||
return SiteMap() | ||
} | ||
|
||
// See SwiftPackageIndex-Server#2485 for context on the performance implications of this | ||
let lastmod: Node<SiteMap.URLContext> = lastActivityAt.map { .lastmod($0, timeZone: .utc) } ?? .empty | ||
|
||
return SiteMap( | ||
.url( | ||
.loc(SiteURL.package(.value(owner), | ||
.value(repository), | ||
.none).absoluteURL()), | ||
lastmod | ||
), | ||
.forEach(linkablePathUrls, { url in | ||
.url(.loc(url), lastmod) | ||
}) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 19 additions & 1 deletion
20
Tests/AppTests/__Snapshots__/SitemapTests/test_siteMapIndex.1.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>http://localhost:8080/sitemap-static-pages.xml</loc><lastmod>1970-01-01</lastmod></sitemap><sitemap><loc>http://localhost:8080/foo/0/sitemap.xml</loc><lastmod>1970-01-01</lastmod></sitemap><sitemap><loc>http://localhost:8080/foo/1/sitemap.xml</loc><lastmod>1970-01-01</lastmod></sitemap><sitemap><loc>http://localhost:8080/foo/2/sitemap.xml</loc><lastmod>1970-01-01</lastmod></sitemap></sitemapindex> | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
<sitemap> | ||
<loc>http://localhost:8080/sitemap-static-pages.xml</loc> | ||
<lastmod>1970-01-01</lastmod> | ||
</sitemap> | ||
<sitemap> | ||
<loc>http://localhost:8080/foo/0/sitemap.xml</loc> | ||
<lastmod>1970-01-01</lastmod> | ||
</sitemap> | ||
<sitemap> | ||
<loc>http://localhost:8080/foo/1/sitemap.xml</loc> | ||
<lastmod>1970-01-01</lastmod> | ||
</sitemap> | ||
<sitemap> | ||
<loc>http://localhost:8080/foo/2/sitemap.xml</loc> | ||
<lastmod>1970-01-01</lastmod> | ||
</sitemap> | ||
</sitemapindex> |
22 changes: 21 additions & 1 deletion
22
Tests/AppTests/__Snapshots__/SitemapTests/test_siteMapStaticPages.1.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"><url><loc>http://localhost:8080/</loc></url><url><loc>http://localhost:8080/add-a-package</loc></url><url><loc>http://localhost:8080/faq</loc></url><url><loc>http://localhost:8080/supporters</loc></url><url><loc>http://localhost:8080/build-monitor</loc></url><url><loc>http://localhost:8080/privacy</loc></url></urlset> | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"> | ||
<url> | ||
<loc>http://localhost:8080/</loc> | ||
</url> | ||
<url> | ||
<loc>http://localhost:8080/add-a-package</loc> | ||
</url> | ||
<url> | ||
<loc>http://localhost:8080/faq</loc> | ||
</url> | ||
<url> | ||
<loc>http://localhost:8080/supporters</loc> | ||
</url> | ||
<url> | ||
<loc>http://localhost:8080/build-monitor</loc> | ||
</url> | ||
<url> | ||
<loc>http://localhost:8080/privacy</loc> | ||
</url> | ||
</urlset> |