Skip to content

Commit

Permalink
Merge pull request #209 from Cloud-Code-AI/208-cloudflare-deployment-fix
Browse files Browse the repository at this point in the history
fix: updated build for cloudflare pages
  • Loading branch information
sauravpanda authored Dec 12, 2024
2 parents 2705ab3 + 6ebacc9 commit d710430
Show file tree
Hide file tree
Showing 16 changed files with 300 additions and 280 deletions.
7 changes: 4 additions & 3 deletions docs/package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{
"name": "akiradocs",
"version": "1.0.46",
"version": "1.0.47",
"private": true,
"scripts": {
"translate": "npm run compile && node scripts/translate.js",
"compile": "node scripts/compile.js",
"compile": "node scripts/compile.js && npm run generate-manifest",
"generate-sitemap": "node scripts/generate-sitemap.mjs",
"dev": "npm run compile && set NEXT_PUBLIC_AKIRADOCS_EDIT_MODE=true && next dev",
"build": "npm run compile && npm run generate-sitemap && set NEXT_PUBLIC_AKIRADOCS_EDIT_MODE=false && next build",
"start": "set NEXT_PUBLIC_AKIRADOCS_EDIT_MODE=false && next start",
"lint": "next lint",
"migrate-gitbook": "node scripts/migrations/gitbook.js"
"migrate-gitbook": "node scripts/migrations/gitbook.js",
"generate-manifest": "node scripts/generate-manifest.js"
},
"dependencies": {
"@ai-sdk/anthropic": "^1.0.1",
Expand Down
28 changes: 28 additions & 0 deletions docs/scripts/generate-manifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const fs = require('fs');
const path = require('path');

function getAllFiles(dirPath, arrayOfFiles = []) {
const files = fs.readdirSync(dirPath);

files.forEach(file => {
const fullPath = path.join(dirPath, file);
if (fs.statSync(fullPath).isDirectory()) {
getAllFiles(fullPath, arrayOfFiles);
} else {
arrayOfFiles.push(
fullPath.replace(path.join(process.cwd(), 'compiled/'), '')
);
}
});

return arrayOfFiles;
}

const manifest = {
files: getAllFiles(path.join(process.cwd(), 'compiled'))
};

fs.writeFileSync(
path.join(process.cwd(), 'compiled/manifest.json'),
JSON.stringify(manifest, null, 2)
);
48 changes: 23 additions & 25 deletions docs/src/app/[locale]/[type]/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ import { PageNavigation } from '@/components/layout/PageNavigation'
import { MainTitle, SubTitle } from '@/components/blocks/HeadingBlock'
import { SEO } from '@/components/layout/SEO'
import { NotFound } from '@/components/layout/NotFound'
import { TextToSpeech } from '@/components/tts/TextToSpeech'
import { getAkiradocsConfig } from "@/lib/getAkiradocsConfig";
import { getTranslation } from '@/lib/staticTranslation';
import { ClientSideControls } from '@/components/layout/ClientSideControl';
import { Metadata } from 'next'

// export const runtime = 'edge'
export const dynamic = 'force-static';
export const runtime = 'edge'
// export const dynamic = 'force-static';

const PostContainer = ({ children }: { children: React.ReactNode }) => (
<div className="flex-1 min-w-0 px-8 py-6 mx-4 font-sans leading-relaxed relative">
Expand All @@ -36,33 +35,33 @@ type Props = {
}>;
}

export async function generateStaticParams() {
const locales = ['en', 'es', 'fr'];
const types = ['docs', 'api', 'articles'];
const allSlugs: { locale: string, type: string, slug: string[] }[] = [];
// export async function generateStaticParams() {
// const locales = ['en', 'es', 'fr'];
// const types = ['docs', 'api', 'articles'];
// const allSlugs: { locale: string, type: string, slug: string[] }[] = [];

locales.forEach(locale => {
types.forEach(type => {
const navigationItems = getContentNavigation({}, locale, type);
if (Array.isArray(navigationItems)) {
navigationItems.forEach(item => {
if (item.slug) {
allSlugs.push({ locale, type, slug: item.slug.split('/') });
}
});
}
});
});
// locales.forEach(locale => {
// types.forEach(type => {
// const navigationItems = getContentNavigation({}, locale, type);
// if (Array.isArray(navigationItems)) {
// navigationItems.forEach(item => {
// if (item.slug) {
// allSlugs.push({ locale, type, slug: item.slug.split('/') });
// }
// });
// }
// });
// });

return allSlugs;
}
// return allSlugs;
// }

export async function generateMetadata({ params }: Props): Promise<Metadata> {
const resolvedParams = await Promise.resolve(params);
const { locale, type, slug: slugArray } = resolvedParams;

const slug = slugArray.length ? slugArray.join('/') : '';
const post = getContentBySlug(locale, type, slug);
const post = await getContentBySlug(locale, type, slug);
const t = getTranslation(locale as 'en' | 'es' | 'fr');
const akiradocsConfig = getAkiradocsConfig();

Expand Down Expand Up @@ -94,15 +93,14 @@ export default async function ContentPage({ params }: Props) {
const t = getTranslation(locale as 'en' | 'es' | 'de');

const slug = slugArray.length ? slugArray.join('/') : '';
const post = getContentBySlug(locale, type, slug);

const post = await getContentBySlug(locale, type, slug);
if (!post) {
return <NotFound redirectUrl={`/${locale}/${type}`} />;
}
const akiradocsConfig = getAkiradocsConfig();
const headerConfig = getHeaderConfig();
const footerConfig = getFooterConfig();
const navigationItems = getContentNavigation({}, locale, type);
const navigationItems = await getContentNavigation({}, locale, type);
const { prev, next } = getNextPrevPages(navigationItems, `/${type}/${slug}`);
const pageTitle = t(post.title) || t('common.documentation');
const pageDescription = t(post.description) || t('common.documentationContent');
Expand Down
19 changes: 17 additions & 2 deletions docs/src/app/[locale]/[type]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,33 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
}
}

function formatRedirectUrl(locale: string, type: string, slug: string): string {
// Remove leading/trailing slashes
const cleanSlug = slug.replace(/^\/+|\/+$/g, '');

// Check if slug already contains locale and/or type
const parts = cleanSlug.split('/');
const slugWithoutLocaleAndType = parts
.filter(part => part !== locale && part !== type)
.join('/');

return `/${locale}/${type}/${slugWithoutLocaleAndType}`;
}

export default async function Page({ params }: Props) {
const resolvedParams = await Promise.resolve(params);
const { locale, type } = resolvedParams;

// Get the first/default content for this type
const recentContent = getRecentContent(`${locale}/${type}`);
const recentContent = await getRecentContent(`${locale}/${type}`);

if (recentContent) {
const redirectUrl = `/${locale}/${type}/${recentContent.slug.replace(`${type}/`, '')}`;
const redirectUrl = formatRedirectUrl(locale, type, recentContent.slug);
redirect(redirectUrl);
}

// Fallback redirect if no content found
console.log("redirecting to", `/${locale}`);
redirect(`/${locale}`);
}

2 changes: 1 addition & 1 deletion docs/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function generateMetadata() {
export default async function DocPage() {
const config = getAkiradocsConfig()
const defaultLocale = config.localization.defaultLocale
const recentContent = getRecentContent(`${defaultLocale}/docs`)
const recentContent = await getRecentContent(`${defaultLocale}/docs`)

if (recentContent) {
const redirectUrl = `/${defaultLocale}/docs/${recentContent.slug.replace('docs/', '')}`
Expand Down
4 changes: 2 additions & 2 deletions docs/src/components/layout/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ const NavItem = React.memo(({ locale, item, pathname, depth = 0 }: NavItemProps)

NavItem.displayName = 'NavItem'

export function ApiSidebar() {
const navigation = getApiNavigation();
export async function ApiSidebar() {
const navigation = await getApiNavigation();

return (
<ErrorBoundary FallbackComponent={ErrorFallback}>
Expand Down
Loading

0 comments on commit d710430

Please sign in to comment.