Skip to content
This repository was archived by the owner on Oct 13, 2022. It is now read-only.

Commit b01eb6d

Browse files
authored
Merge pull request #49 from sveltejs/basepath
Use <base>, via %sapper.base%
2 parents 99445fa + b904695 commit b01eb6d

File tree

8 files changed

+18
-24
lines changed

8 files changed

+18
-24
lines changed

app/server.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fs from 'fs';
1+
import { resolve } from 'url';
22
import polka from 'polka';
33
import compression from 'compression';
44
import sapper from 'sapper';
@@ -10,13 +10,11 @@ const { PORT } = process.env;
1010

1111
// this allows us to do e.g. `fetch('/api/blog-posts')` on the server
1212
global.fetch = (url, opts) => {
13-
if (url[0] === '/') url = `http://localhost:${PORT}${url}`;
13+
url = resolve(`http://localhost:${PORT}/`, url);
1414
return fetch(url, opts);
1515
};
1616

17-
// you can also use Express
1817
polka()
1918
.use(compression({ threshold: 0 }))
20-
.use(serve('assets'))
21-
.use(sapper({ routes }))
19+
.use(serve('assets'), sapper({ routes }))
2220
.listen(PORT);

app/template.html

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@
55
<meta name='viewport' content='width=device-width'>
66
<meta name='theme-color' content='#aa1e1e'>
77

8-
<link rel='stylesheet' href='/global.css'>
9-
<link rel='manifest' href='/manifest.json'>
10-
<link rel='icon' type='image/png' href='/favicon.png'>
8+
%sapper.base%
119

12-
<script>
13-
if ('serviceWorker' in navigator) {
14-
navigator.serviceWorker.register('/service-worker.js');
15-
}
16-
</script>
10+
<link rel='stylesheet' href='global.css'>
11+
<link rel='manifest' href='manifest.json'>
12+
<link rel='icon' type='image/png' href='favicon.png'>
1713

1814
<!-- Sapper generates a <style> tag containing critical CSS
1915
for the current page. CSS for the rest of the app is

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"polka": "^0.3.4",
2020
"sapper": "^0.9.4",
2121
"serve-static": "^1.13.1",
22-
"svelte": "^1.56.0",
22+
"svelte": "^1.57.3",
2323
"svelte-loader": "^2.3.3",
2424
"webpack": "^4.1.0"
2525
}

routes/_components/Nav.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<nav>
22
<ul>
3-
<li><a class='{{page === "home" ? "selected" : ""}}' href='/'>home</a></li>
4-
<li><a class='{{page === "about" ? "selected" : ""}}' href='/about'>about</a></li>
3+
<li><a class='{{page === "home" ? "selected" : ""}}' href='.'>home</a></li>
4+
<li><a class='{{page === "about" ? "selected" : ""}}' href='about'>about</a></li>
55

66
<!-- for the blog link, we're using rel=prefetch so that Sapper prefetches
77
the blog data when we hover over the link or tap it on a touchscreen -->
8-
<li><a rel=prefetch class='{{page === "blog" ? "selected" : ""}}' href='/blog'>blog</a></li>
8+
<li><a rel=prefetch class='{{page === "blog" ? "selected" : ""}}' href='blog'>blog</a></li>
99
</ul>
1010
</nav>
1111

routes/blog/[slug].html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ <h1>{{post.title}}</h1>
5959
// is called [slug].html
6060
const { slug } = params;
6161

62-
return fetch(`/blog/${slug}.json`).then(r => r.json()).then(post => {
62+
return fetch(`blog/${slug}.json`).then(r => r.json()).then(post => {
6363
return { post };
6464
});
6565
}

routes/blog/_posts.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const posts = [
1414
html: `
1515
<p>First, you have to know what <a href='https://svelte.technology'>Svelte</a> is. Svelte is a UI framework with a bold new idea: rather than providing a library that you write code with (like React or Vue, for example), it's a compiler that turns your components into highly optimized vanilla JavaScript. If you haven't already read the <a href='https://svelte.technology/blog/frameworks-without-the-framework'>introductory blog post</a>, you should!</p>
1616
17-
<p>Sapper is a Next.js-style framework (<a href='/blog/how-is-sapper-different-from-next'>more on that here</a>) built around Svelte. It makes it embarrassingly easy to create extremely high performance web apps. Out of the box, you get:</p>
17+
<p>Sapper is a Next.js-style framework (<a href='blog/how-is-sapper-different-from-next'>more on that here</a>) built around Svelte. It makes it embarrassingly easy to create extremely high performance web apps. Out of the box, you get:</p>
1818
1919
<ul>
2020
<li>Code-splitting, dynamic imports and hot module replacement, powered by webpack</li>
@@ -70,8 +70,8 @@ const posts = [
7070
<ul>
7171
<li>It's powered by <a href='https://svelte.technology'>Svelte</a> instead of React, so it's faster and your apps are smaller</li>
7272
<li>Instead of route masking, we encode route parameters in filenames. For example, the page you're looking at right now is <code>routes/blog/[slug].html</code></li>
73-
<li>As well as pages (Svelte components, which render on server or client), you can create <em>server routes</em> in your <code>routes</code> directory. These are just <code>.js</code> files that export functions corresponding to HTTP methods, and receive Express <code>request</code> and <code>response</code> objects as arguments. This makes it very easy to, for example, add a JSON API such as the one <a href='/blog/how-is-sapper-different-from-next.json'>powering this very page</a></li>
74-
<li>Links are just <code>&lt;a&gt;</code> elements, rather than framework-specific <code>&lt;Link&gt;</code> components. That means, for example, that <a href='/blog/how-can-i-get-involved'>this link right here</a>, despite being inside a blob of HTML, works with the router as you'd expect.</li>
73+
<li>As well as pages (Svelte components, which render on server or client), you can create <em>server routes</em> in your <code>routes</code> directory. These are just <code>.js</code> files that export functions corresponding to HTTP methods, and receive Express <code>request</code> and <code>response</code> objects as arguments. This makes it very easy to, for example, add a JSON API such as the one <a href='blog/how-is-sapper-different-from-next.json'>powering this very page</a></li>
74+
<li>Links are just <code>&lt;a&gt;</code> elements, rather than framework-specific <code>&lt;Link&gt;</code> components. That means, for example, that <a href='blog/how-can-i-get-involved'>this link right here</a>, despite being inside a blob of HTML, works with the router as you'd expect.</li>
7575
</ul>
7676
`
7777
},

routes/blog/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ <h1>Recent posts</h1>
1111
tell Sapper to load the data for the page as soon as
1212
the user hovers over the link or taps it, instead of
1313
waiting for the 'click' event -->
14-
<li><a rel='prefetch' href='/blog/{{post.slug}}'>{{post.title}}</a></li>
14+
<li><a rel='prefetch' href='blog/{{post.slug}}'>{{post.title}}</a></li>
1515
{{/each}}
1616
</ul>
1717
</Layout>
@@ -32,7 +32,7 @@ <h1>Recent posts</h1>
3232
},
3333

3434
preload({ params, query }) {
35-
return fetch(`/blog.json`).then(r => r.json()).then(posts => {
35+
return fetch(`blog.json`).then(r => r.json()).then(posts => {
3636
return { posts };
3737
});
3838
}

routes/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<h1>Great success!</h1>
77

88
<figure>
9-
<img alt='Borat' src='/great-success.png'>
9+
<img alt='Borat' src='great-success.png'>
1010
<figcaption>HIGH FIVE!</figcaption>
1111
</figure>
1212

0 commit comments

Comments
 (0)