Skip to content

Commit

Permalink
Do not encourage the use of a leading slash
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 21, 2022
1 parent 4cc720f commit 9796d8f
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 28 deletions.
20 changes: 10 additions & 10 deletions docs/Cheatsheet.md
Expand Up @@ -144,7 +144,7 @@ ctx.log.debug('Shut up and take my money!');
const url = ctx.urlFor('index');

// urlForFile: generate URL for static file
const url = ctx.urlForFile('/foo/app.css');
const url = ctx.urlForFile('foo/app.css');

// urlWith: generate URL for route or path and preserve the current query parameters
const url = ctx.urlWith('index');
Expand Down Expand Up @@ -436,8 +436,8 @@ These view helpers are currently available by default:
#### assetTag

```
%= ctx.assetTag('/app.js')
%= ctx.assetTag('/app.js', {async: 'async'})
%= ctx.assetTag('app.js')
%= ctx.assetTag('app.js', {async: 'async'})
```

Generate `<script>`, `<link>` or `<img>` tag for static asset.
Expand All @@ -464,7 +464,7 @@ Generate `<input>` tag of type `checkbox`. Previous input values will automatica

```
%= ctx.faviconTag()
%= ctx.faviconTag('/favicon.ico')
%= ctx.faviconTag('favicon.ico')
```

Generate `<link>` tag for a favison, defaults to the [mojo.js](https://mojojs.org) favicon.
Expand All @@ -483,8 +483,8 @@ be added as well.
#### imageTag

```
%= ctx.imageTag('/myapp/logo.png')
%= ctx.imageTag('/myapp/logo.png', {alt: 'just a logo'})
%= ctx.imageTag('myapp/logo.png')
%= ctx.imageTag('myapp/logo.png', {alt: 'just a logo'})
```

Generate `<img>` tag for image file.
Expand Down Expand Up @@ -527,17 +527,17 @@ Generate `<input>` tag of type `radio`. Previous input values will automatically
#### scriptTag

```
%= ctx.scriptTag('/bootstrap/bootstrap.bundle.min.js')
%= ctx.scriptTag('/bootstrap/bootstrap.bundle.min.js', {async: 'async'})
%= ctx.scriptTag('bootstrap/bootstrap.bundle.min.js')
%= ctx.scriptTag('bootstrap/bootstrap.bundle.min.js', {async: 'async'})
```

Generate `<script>` tag for JavaScript file.

#### styleTag

```
%= ctx.styleTag('/bootstrap/bootstrap.min.css')
%= ctx.styleTag('/bootstrap/bootstrap.min.css', {media: 'foo'})
%= ctx.styleTag('bootstrap/bootstrap.min.css')
%= ctx.styleTag('bootstrap/bootstrap.min.css', {media: 'foo'})
```

Generate `<link>` tag for CSS file.
Expand Down
2 changes: 1 addition & 1 deletion docs/Introduction.md
Expand Up @@ -706,7 +706,7 @@ const app = mojo();

// Redirect to static file
app.get('/file', async ctx => {
const url = ctx.urlForFile('/test.txt');
const url = ctx.urlForFile('test.txt');
await ctx.redirectTo(url, {status: 301});
});

Expand Down
8 changes: 4 additions & 4 deletions docs/Rendering.md
Expand Up @@ -337,7 +337,7 @@ prefix is dynamic, you can use `ctx.urlForFile()` to generate URLs.
```js
// "/static/foo/bar.txt"
ctx.urlForFile('/foo/bar.txt');
ctx.urlForFile('foo/bar.txt');
```
### Static Assets
Expand All @@ -350,10 +350,10 @@ then use `ctx.urlForAsset()` or `ctx.assetTag()` to generate URLs without having
```js
// "/static/assets/myapp.ab1234cd5678ef.js"
ctx.urlForAsset('/myapp.js');
ctx.urlForAsset('myapp.js');

// "<script src="/static/assets/myapp.ab1234cd5678ef.js"></script>"
ctx.assetTag('/myapp.js');
ctx.assetTag('myapp.js');
```
If your application runs in `development` mode, all assets will be served with a `Cache-Control: no-cache` header, to
Expand All @@ -364,7 +364,7 @@ each time they are rebuilt with a different checksum.
```js
// "/static/assets/foo/bar.development.js"
ctx.urlForAsset('/foo/bar.js');
ctx.urlForAsset('foo/bar.js');
```
Webpack [configuration](https://webpack.js.org/configuration/) example (`webpack.config.js`):
Expand Down
2 changes: 1 addition & 1 deletion src/cli/create-full-app.ts
Expand Up @@ -165,7 +165,7 @@ const tmplView = `%% view.layout = 'default';
<p>
This page was generated from the template "views/example/welcome.html.tmpl" and the layout
"views/layouts/default.html.tmpl", <a href="<%%= ctx.urlFor() %>">click here</a> to reload the page or
<a href="<%%= ctx.urlForFile('/index.html') %>">here</a> to move forward to a static page.
<a href="<%%= ctx.urlForFile('index.html') %>">here</a> to move forward to a static page.
</p>
`;

Expand Down
4 changes: 2 additions & 2 deletions test/static-app.js
Expand Up @@ -267,11 +267,11 @@ t.test('Static app', async t => {
await t.test('Assets', async t => {
const ctx = app.newMockContext();
t.equal(ctx.urlForAsset('http://example.com'), 'http://example.com');
t.equal(ctx.urlForAsset('/unknown.css'), '/static/assets/unknown.css');
t.equal(ctx.urlForAsset('unknown.css'), '/static/assets/unknown.css');
t.equal(ctx.urlForAsset('foo.css'), '/static/assets/foo.ab1234cd5678ef.css');
t.equal(ctx.urlForAsset('/foo.css'), '/static/assets/foo.ab1234cd5678ef.css');
t.equal(ctx.urlForAsset('/foo.js'), '/static/assets/foo.ab1234cd5678ef.js');
t.equal(ctx.urlForAsset('/foo/bar/baz.js'), '/static/assets/foo/bar/baz.development.js');
t.equal(ctx.urlForAsset('foo/bar/baz.js'), '/static/assets/foo/bar/baz.development.js');
t.equal(ctx.urlForAsset('/foo/bar.js'), '/static/assets/foo/bar.321.js');
t.equal(ctx.urlForAsset('/foo/bar/test.min.js'), '/static/assets/foo/bar/test.ab1234cd5678ef.min.js');
t.equal(ctx.urlForAsset('/foo/bar/yada.css'), '/static/assets/foo/bar/yada.css');
Expand Down
14 changes: 7 additions & 7 deletions vendor/views/mojo/debug.html.tmpl
Expand Up @@ -4,12 +4,12 @@
<head>
<title>Debug</title>
%= ctx.faviconTag()
%= ctx.scriptTag('/mojo/bootstrap/bootstrap.bundle.min.js')
%= ctx.scriptTag('/mojo/highlight.js/highlight.pack.js')
%= ctx.styleTag('/mojo/bootstrap/bootstrap.min.css')
%= ctx.styleTag('/mojo/highlight.js/highlight-mojo-dark.css')
%= ctx.scriptTag('mojo/bootstrap/bootstrap.bundle.min.js')
%= ctx.scriptTag('mojo/highlight.js/highlight.pack.js')
%= ctx.styleTag('mojo/bootstrap/bootstrap.min.css')
%= ctx.styleTag('mojo/highlight.js/highlight-mojo-dark.css')
%= ctx.styleTag('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css')
%= ctx.styleTag('/mojo/mojo.css')
%= ctx.styleTag('mojo/mojo.css')
<script>
hljs.initHighlightingOnLoad();
window.onload = function() {
Expand All @@ -26,8 +26,8 @@
<div class="container-fluid">
<a href="https://mojojs.org" id="mojobar-brand" class="navbar-brand">
<picture>
<img src="<%= ctx.urlForFile('/mojo/logo-white.png') %>"
srcset="<%= ctx.urlForFile('/mojo/logo-white-2x.png') %> 2x">
<img src="<%= ctx.urlForFile('mojo/logo-white.png') %>"
srcset="<%= ctx.urlForFile('mojo/logo-white-2x.png') %> 2x">
</picture>
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarContent"
Expand Down
2 changes: 1 addition & 1 deletion vendor/views/mojo/exception.html.tmpl
Expand Up @@ -9,7 +9,7 @@
background-color: #caecf6;
}
#raptor {
background: url(<%= ctx.urlForFile('/mojo/failraptor.png') %>);
background: url(<%= ctx.urlForFile('mojo/failraptor.png') %>);
height: 488px;
left: 50%;
margin-left: -371px;
Expand Down
4 changes: 2 additions & 2 deletions vendor/views/mojo/not-found.html.tmpl
Expand Up @@ -17,7 +17,7 @@
top: 60%;
}
#not-found {
background: url(<%= ctx.urlForFile('/mojo/not-found.png') %>);
background: url(<%= ctx.urlForFile('mojo/not-found.png') %>);
height: 62px;
left: 50%;
margin-left: -153px;
Expand All @@ -30,7 +30,7 @@
</head>
<body>
<a href="<%= ctx.req.baseURL %>">
<img src="<%= ctx.urlForFile('/mojo/no-raptor.png') %>" alt="Bye!" id="no-raptor">
<img src="<%= ctx.urlForFile('mojo/no-raptor.png') %>" alt="Bye!" id="no-raptor">
</a>
<div id="not-found"></div>
</body>
Expand Down

0 comments on commit 9796d8f

Please sign in to comment.