Skip to content

Commit 0bedd26

Browse files
committed
fix: support a base different from / in astro config
1 parent 6d01620 commit 0bedd26

File tree

10 files changed

+20
-12
lines changed

10 files changed

+20
-12
lines changed

packages/astro/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@tutorialkit/runtime": "workspace:*",
3030
"@tutorialkit/types": "workspace:*",
3131
"@types/react": "^18.3.3",
32+
"@unocss/transformer-directives": "^0.59.4",
3233
"@webcontainer/api": "1.2.0",
3334
"astro": "^4.10.3",
3435
"astro-expressive-code": "^0.35.3",

packages/astro/src/default/components/Logo.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function readLogoFile(logoPrefix: string) {
1616
const exists = fs.existsSync(path.join('public', logoFilename));
1717
1818
if (exists) {
19-
logo = `/${logoFilename}`;
19+
logo = `${import.meta.env.BASE_URL}/${logoFilename}`;
2020
break;
2121
}
2222
}

packages/astro/src/default/components/webcontainer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ if (!import.meta.env.SSR) {
2020
});
2121
}
2222

23-
export const tutorialStore = new TutorialStore({ webcontainer, useAuth });
23+
export const tutorialStore = new TutorialStore({ webcontainer, useAuth, baseURL: import.meta.env.BASE_URL });
2424

2525
export async function login() {
2626
auth.startAuthFlow({ popup: true });

packages/astro/src/default/layouts/Layout.astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ interface Props {
66
}
77
88
const { title } = Astro.props;
9+
const baseURL = import.meta.env.BASE_URL;
910
---
1011

1112
<!doctype html>
@@ -16,7 +17,7 @@ const { title } = Astro.props;
1617
<meta name="viewport" content="width=device-width" />
1718
<meta name="generator" content={Astro.generator} />
1819
<title>{title}</title>
19-
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
20+
<link rel="icon" type="image/svg+xml" href={`${baseURL}/favicon.svg`} />
2021
<link rel="preconnect" href="https://fonts.googleapis.com" />
2122
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
2223
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet" />

packages/astro/src/default/pages/index.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const part = tutorial.parts[tutorial.firstPartId!];
77
const chapter = part.chapters[part?.firstChapterId!];
88
const lesson = chapter.lessons[chapter?.firstLessonId!];
99
10-
const redirect = `/${part.slug}/${chapter.slug}/${lesson.slug}`;
10+
const redirect = `${import.meta.env.BASE_URL}/${part.slug}/${chapter.slug}/${lesson.slug}`;
1111
---
1212

1313
<!doctype html>

packages/astro/src/default/utils/content.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ export async function getTutorial(): Promise<Tutorial> {
226226
return 0;
227227
});
228228

229+
const baseURL = import.meta.env.BASE_URL;
230+
229231
// now we link all lessons together
230232
for (const [i, lesson] of lessons.entries()) {
231233
const prevLesson = i > 0 ? lessons.at(i - 1) : undefined;
@@ -248,7 +250,7 @@ export async function getTutorial(): Promise<Tutorial> {
248250

249251
lesson.prev = {
250252
title: prevLesson.data.title,
251-
href: `/${partSlug}/${chapterSlug}/${prevLesson.slug}`,
253+
href: `${baseURL}/${partSlug}/${chapterSlug}/${prevLesson.slug}`,
252254
};
253255
}
254256

@@ -258,7 +260,7 @@ export async function getTutorial(): Promise<Tutorial> {
258260

259261
lesson.next = {
260262
title: nextLesson.data.title,
261-
href: `/${partSlug}/${chapterSlug}/${nextLesson.slug}`,
263+
href: `${baseURL}/${partSlug}/${chapterSlug}/${nextLesson.slug}`,
262264
};
263265
}
264266
}

packages/astro/src/default/utils/nav.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Tutorial, NavList } from '@tutorialkit/types';
22

3-
export function generateNavigationList(tutorial: Tutorial): NavList {
3+
export function generateNavigationList(tutorial: Tutorial, baseURL: string): NavList {
44
return objectToSortedArray(tutorial.parts).map((part) => {
55
return {
66
id: part.id,

packages/astro/src/default/utils/routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export async function generateStaticRoutes() {
2424
},
2525
props: {
2626
logoLink: tutorial.logoLink,
27-
navList: generateNavigationList(tutorial),
27+
navList: generateNavigationList(tutorial, import.meta.env.BASE_URL),
2828
title: `${part.data.title} / ${chapter.data.title} / ${lesson.data.title}`,
2929
lesson: lesson as Lesson<AstroComponentFactory>,
3030
},

packages/runtime/src/lesson-files.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export class LessonFilesFetcher {
1414
private _templateLoadTask?: Task<Files>;
1515
private _templateLoaded: string | undefined;
1616

17+
constructor(private _baseURL: string = '') {}
18+
1719
async invalidate(filesRef: string): Promise<InvalidationResult> {
1820
if (!this._map.has(filesRef)) {
1921
return { type: 'none' };
@@ -63,7 +65,7 @@ export class LessonFilesFetcher {
6365
this._templateLoadTask?.cancel();
6466

6567
const task = newTask(async (signal) => {
66-
const response = await fetch(`/${templatePathname}`, { signal });
68+
const response = await fetch(`${this._baseURL}/${templatePathname}`, { signal });
6769

6870
if (!response.ok) {
6971
throw new Error(`Failed to fetch: status ${response.status}`);
@@ -106,7 +108,7 @@ export class LessonFilesFetcher {
106108

107109
while (true) {
108110
try {
109-
const response = await fetch(`/${pathname}`);
111+
const response = await fetch(`${this._baseURL}/${pathname}`);
110112

111113
if (!response.ok) {
112114
throw new Error(`Failed to fetch ${pathname}: ${response.status} ${response.statusText}`);

packages/runtime/src/store/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { TerminalStore } from './terminal.js';
1515
interface StoreOptions {
1616
webcontainer: Promise<WebContainer>;
1717
useAuth: boolean;
18+
baseURL?: string;
1819
}
1920

2021
export class TutorialStore {
@@ -26,7 +27,7 @@ export class TutorialStore {
2627
private _terminalStore: TerminalStore;
2728

2829
private _stepController = new StepsController();
29-
private _lessonFilesFetcher = new LessonFilesFetcher();
30+
private _lessonFilesFetcher: LessonFilesFetcher;
3031
private _lessonTask: Task<unknown> | undefined;
3132
private _lesson: Lesson | undefined;
3233
private _ref: number = 1;
@@ -42,9 +43,10 @@ export class TutorialStore {
4243
*/
4344
readonly lessonFullyLoaded = atom<boolean>(false);
4445

45-
constructor({ useAuth, webcontainer }: StoreOptions) {
46+
constructor({ useAuth, webcontainer, baseURL }: StoreOptions) {
4647
this._webcontainer = webcontainer;
4748
this._editorStore = new EditorStore();
49+
this._lessonFilesFetcher = new LessonFilesFetcher(baseURL);
4850
this._previewsStore = new PreviewsStore(this._webcontainer);
4951
this._terminalStore = new TerminalStore(this._webcontainer, useAuth);
5052
this._runner = new TutorialRunner(this._webcontainer, this._terminalStore, this._stepController);

0 commit comments

Comments
 (0)