Skip to content

Commit e729c16

Browse files
committed
Get production compatible with netlify
- Update netlify.toml to run build via npm - Added note to README about serving production locally - Updated dev instructions on search component to use netlify-cli - Updated _headers to get rid of header errors - Added guard to Search component due to known issue with netlify adapter Signed-off-by: Jake Bellacera <hi@jakebellacera.com>
1 parent 49919e4 commit e729c16

5 files changed

Lines changed: 38 additions & 20 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ pnpm-debug.log*
2828

2929
# Netlify
3030
.netlify
31+
deno.lock

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ Finally, you can run a local Astro dev server by running the following command:
4747
npm run dev
4848
```
4949

50+
### Developing for production
51+
52+
If you need to develop against a production build (for example, testing search functionality), you can do so with [netlify-cli](https://cli.netlify.com/).
53+
54+
```sh
55+
netlify serve
56+
```
57+
58+
This will build a production version of the site and serve it locally.
59+
5060
### Code formatting
5161

5262
If you use Visual Studio Code, install the [Prettier](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode) and [ESLint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) extensions to automatically format your code as you make changes.

netlify.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[build]
22
base = "."
33
publish = "/dist"
4-
command = "pnpm build"
4+
command = "npm run build"

public/_headers

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Content-Security-Policy: default-src 'self'
2-
X-Frame-Options: DENY
3-
X-Content-Type-Options: nosniff
4-
Referrer-Policy: strict-origin-when-cross-origin
1+
/*
2+
X-Frame-Options: DENY
3+
X-Content-Type-Options: nosniff
4+
Referrer-Policy: strict-origin-when-cross-origin

src/components/Header/Search.astro

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import HeaderButton from "./HeaderButton.astro";
2424
<p>To view search results, run a production build:</p>
2525
<p>
2626
<code class="text-white bg-darkBorder rounded px-3 py-2 ml-3">
27-
$ pnpm build && pnpm preview
27+
$ netlify serve
2828
</code>
2929
</p>
3030
</Typography>
@@ -145,19 +145,6 @@ import HeaderButton from "./HeaderButton.astro";
145145
position: static;
146146
}
147147
</style>
148-
<script is:inline>
149-
(() => {
150-
const openBtn = document.querySelector(".header-open-search-modal");
151-
const shortcut = openBtn?.querySelector("kbd");
152-
if (!openBtn || !(shortcut instanceof HTMLElement)) return;
153-
const platformKey = shortcut.querySelector("kbd");
154-
if (platformKey && /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform)) {
155-
platformKey.textContent = "⌘";
156-
openBtn.setAttribute("aria-keyshortcuts", "Meta+K");
157-
}
158-
shortcut.style.display = "";
159-
})();
160-
</script>
161148
<script>
162149
class SearchContainer extends HTMLElement {
163150
#modal: HTMLDialogElement | null = null;
@@ -184,6 +171,16 @@ import HeaderButton from "./HeaderButton.astro";
184171
}
185172

186173
connectedCallback() {
174+
const shortcut = this.#button?.querySelector("kbd");
175+
if (shortcut instanceof HTMLElement) {
176+
const platformKey = shortcut.querySelector("kbd");
177+
if (platformKey && /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform)) {
178+
platformKey.textContent = "⌘";
179+
this.#button?.setAttribute("aria-keyshortcuts", "Meta+K");
180+
}
181+
shortcut.style.display = "";
182+
}
183+
187184
this.#button?.addEventListener("click", () => {
188185
this.openModal();
189186
});
@@ -220,5 +217,15 @@ import HeaderButton from "./HeaderButton.astro";
220217
}
221218
}
222219

223-
customElements.define("search-container", SearchContainer);
220+
/*
221+
The guard (if statement) here is required because it works around a bug with
222+
@astrojs/netlify which is causing this component to get imported twice.
223+
224+
@see https://github.com/withastro/astro/issues/16173
225+
226+
This should be fixed with Astro v6.1.3
227+
*/
228+
if (!customElements.get("search-container")) {
229+
customElements.define("search-container", SearchContainer);
230+
}
224231
</script>

0 commit comments

Comments
 (0)