Skip to content

Commit 69d5359

Browse files
committed
Load cached remote page data from webview
1 parent 4cf783f commit 69d5359

File tree

3 files changed

+51
-45
lines changed

3 files changed

+51
-45
lines changed

common/src/envs/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export const LOCAL_WEB_DOMAIN = `localhost:3000`
4545
export const LOCAL_BACKEND_DOMAIN = `${IS_LOCAL_ANDROID ? '10.0.2.2' : 'localhost'}:8088`
4646

4747
export const DOMAIN = IS_LOCAL ? LOCAL_WEB_DOMAIN : ENV_CONFIG.domain
48+
export const DEPLOYED_WEB_URL = `https://www.${ENV_CONFIG.domain}`
4849
export const WEB_URL = IS_LOCAL ? `http://${LOCAL_WEB_DOMAIN}` : `https://${DOMAIN}`
4950
export const BACKEND_DOMAIN = IS_LOCAL ? LOCAL_BACKEND_DOMAIN : ENV_CONFIG.backendDomain
5051
export const FIREBASE_CONFIG = ENV_CONFIG.firebaseConfig

web/lib/util/page-data.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {DEPLOYED_WEB_URL} from "common/envs/constants";
2+
3+
export const getBuildId = async () => {
4+
const res = await fetch(`${DEPLOYED_WEB_URL}/api/build-id`)
5+
if (!res.ok) throw new Error('Failed to fetch build manifest')
6+
const data = await res.json()
7+
return data.buildId
8+
}
9+
10+
export const getPageData = async (route = '/') => {
11+
const buildId = await getBuildId()
12+
const cleanRoute = route.startsWith('/') ? route.slice(1) : route
13+
const url = `${DEPLOYED_WEB_URL}/api/proxy-data?path=${buildId}/${cleanRoute || 'index'}.json`
14+
console.log('Fetching data from:', url)
15+
const res = await fetch(url, {cache: 'force-cache'}) as any
16+
if (!res.ok) throw new Error(`Failed to fetch data for ${route}`)
17+
const result = await res.json() as any
18+
console.log('Fetched Page data:', result)
19+
return result.pageProps
20+
}

web/pages/[username]/index.tsx

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ import {ProfileInfo} from 'web/components/profile/profile-info'
1616
import {User} from 'common/user'
1717
import {getUserForStaticProps} from 'common/supabase/users'
1818
import {GetStaticPropsContext} from 'next'
19-
import {CompassLoadingIndicator} from "web/components/widgets/loading-indicator";
19+
import {CompassLoadingIndicator} from "web/components/widgets/loading-indicator"
2020
import Custom404 from '../404'
21-
import {sleep} from "common/util/time";
22-
import {isNativeMobile} from "web/lib/util/webview";
23-
import {getPixelHeight} from "web/lib/util/css";
21+
import {sleep} from "common/util/time"
22+
import {isNativeMobile} from "web/lib/util/webview"
23+
import {getPixelHeight} from "web/lib/util/css"
24+
import {getPageData} from "web/lib/util/page-data";
2425

2526
async function getUser(username: string) {
2627
const user = await getUserForStaticProps(db, username)
@@ -39,29 +40,29 @@ async function getProfile(userId: string) {
3940
}
4041
}
4142
console.debug(`Profile loaded after ${i} tries`)
42-
return profile;
43+
return profile
4344
}
4445

4546
// getServerSideProps is a Next.js function that can be used to fetch data and render the contents of a page at request time.
4647
// export async function getServerSideProps(context: any) {
4748
// if (!isNativeMobile()) {
4849
// // Not mobile → let SSG handle it
49-
// return {notFound: true};
50+
// return {notFound: true}
5051
// }
5152
//
5253
// // Mobile path: server-side fetch
53-
// const username = context.params.username;
54-
// const user = await getUser(username);
54+
// const username = context.params.username
55+
// const user = await getUser(username)
5556
//
5657
// if (!user) {
57-
// return {props: {notFoundCustomText: 'User not found'}};
58+
// return {props: {notFoundCustomText: 'User not found'}}
5859
// }
5960
//
60-
// const profile = await getProfile(user.id);
61+
// const profile = await getProfile(user.id)
6162
//
6263
// console.log('getServerSideProps', {user, profile, username})
6364
//
64-
// return {props: {user, profile, username}};
65+
// return {props: {user, profile, username}}
6566
// }
6667

6768
// SSG: static site generation
@@ -154,49 +155,37 @@ export default function UserPage(props: UserPageProps) {
154155
useEffect(() => {
155156
console.log('safe-area-inset-bottom:', getPixelHeight('safe-area-inset-bottom'))
156157
console.log('safe-area-inset-top:', getPixelHeight('safe-area-inset-top'))
157-
}, []);
158+
}, [])
158159

159160
const nativeMobile = isNativeMobile()
160161
const router = useRouter()
161162
const username = (nativeMobile ? router.query.username : props.username) as string
162-
const [user, setUser] = useState<User | undefined>(props.user)
163-
const [profile, setProfile] = useState<ProfileRow | undefined>(props.profile)
164-
const [notFoundCustomText, setNotFoundCustomText] = useState(props.notFoundCustomText)
163+
const [fetchedProps, setFetchedProps] = useState(props)
165164
const [loading, setLoading] = useState(nativeMobile)
166165

167-
console.log('UserPage state:', {username, user, profile, notFoundCustomText, loading, nativeMobile})
166+
console.log('UserPage state:', {username, fetchedProps, loading, nativeMobile})
168167

169168
useEffect(() => {
170169
if (nativeMobile) {
171-
// Mobile/WebView scenario: fetch profile dynamically
170+
// Mobile/WebView scenario: fetch profile dynamically from the remote web server (to benefit from SSR and ISR)
172171
async function load() {
173172
setLoading(true)
174-
const fetchedUser = await getUser(username)
175-
if (!fetchedUser) return
176-
if (fetchedUser.username !== username) {
177-
console.debug('Found a case-insensitive match for native mobile')
178-
await router.push(`/${fetchedUser.username}`)
179-
}
180-
setUser(fetchedUser)
181-
console.debug('fetched user for native mobile:', fetchedUser)
182-
const fetchedProfile = await getProfile(fetchedUser.id)
183-
if (!fetchedProfile) {
184-
setNotFoundCustomText(`${username} hasn't created a profile yet.`)
185-
} else {
186-
setProfile(fetchedProfile)
187-
console.debug('fetched profile for native mobile:', fetchedProfile)
173+
try {
174+
const _props = await getPageData(username)
175+
setFetchedProps(_props)
176+
} catch (e) {
177+
console.error('Failed to fetch profile for native mobile', e)
178+
setFetchedProps({username, notFoundCustomText: 'Failed to fetch profile.'})
188179
}
189180
setLoading(false)
190181
}
191182

192183
load()
193184
} else {
194-
setUser(props.user)
195-
setProfile(props.profile)
196-
setNotFoundCustomText(props.notFoundCustomText)
185+
setFetchedProps(props)
197186
}
198187
// On web, initialProfile from SSR/ISR is already loaded
199-
}, [username, nativeMobile]);
188+
}, [username, nativeMobile])
200189

201190
if (loading) {
202191
return <PageBase
@@ -206,11 +195,11 @@ export default function UserPage(props: UserPageProps) {
206195
</PageBase>
207196
}
208197

209-
if (notFoundCustomText) {
210-
return <Custom404 customText={notFoundCustomText}/>
198+
if (fetchedProps.notFoundCustomText) {
199+
return <Custom404 customText={fetchedProps.notFoundCustomText}/>
211200
}
212201

213-
if (!user) {
202+
if (!fetchedProps.user) {
214203
return <PageBase
215204
trackPageView={'user page'}
216205
className={'relative p-2 sm:pt-0'}
@@ -223,7 +212,7 @@ export default function UserPage(props: UserPageProps) {
223212
</PageBase>
224213
}
225214

226-
if (user.isBannedFromPosting) {
215+
if (fetchedProps.user?.isBannedFromPosting) {
227216
return <PageBase
228217
trackPageView={'user page'}
229218
className={'relative p-2 sm:pt-0'}
@@ -236,7 +225,7 @@ export default function UserPage(props: UserPageProps) {
236225
</PageBase>
237226
}
238227

239-
if (!profile) {
228+
if (!fetchedProps.profile) {
240229
return <PageBase
241230
trackPageView={'user page'}
242231
className={'relative p-2 sm:pt-0'}
@@ -249,11 +238,7 @@ export default function UserPage(props: UserPageProps) {
249238
</PageBase>
250239
}
251240

252-
return <UserPageInner
253-
user={user}
254-
username={username}
255-
profile={profile}
256-
/>
241+
return <UserPageInner {...fetchedProps as ActiveUserPageProps}/>
257242
}
258243

259244
function UserPageInner(props: ActiveUserPageProps) {

0 commit comments

Comments
 (0)