matrix-art

An image gallery for Matrix
git clone git://archive.git.mtrnord.blog/MTRNord/matrix-art.git
Log | Files | Refs | README | LICENSE

commit d68bc192446d346e74209dc6d64107324de98a6c
parent 2aaea866b92c6c1d3c39c2acb68145316ac79d55
Author: MTRNord <mtrnord1@gmail.com>
Date:   Fri, 14 Jan 2022 00:03:11 +0100

Fix server side loading

Diffstat:
Mhelpers/storage.ts | 9+++++++--
Mpages/index.tsx | 27++++++++++++++++++++++-----
Mpages/post/[id].tsx | 16+++++++++++++++-
3 files changed, 44 insertions(+), 8 deletions(-)

diff --git a/helpers/storage.ts b/helpers/storage.ts @@ -5,8 +5,13 @@ export default class Storage { this.prefix = prefix; if (typeof window === "undefined") { var LocalStorage = require('node-localstorage').LocalStorage; // eslint-disable-line unicorn/prefer-module - var path = require('path'); // eslint-disable-line unicorn/prefer-module - this.nodeLocalStorage = new LocalStorage(path.join(process.cwd(), "localstorage")); + var path = require('node:path'); // eslint-disable-line unicorn/prefer-module + var fs = require('node:fs'); // eslint-disable-line unicorn/prefer-module + const dir = "localstorage"; + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir); + } + this.nodeLocalStorage = new LocalStorage(path.join(process.cwd(), dir)); } } getItem(key: string): any { diff --git a/pages/index.tsx b/pages/index.tsx @@ -5,11 +5,11 @@ import Header from '../components/Header'; import { client, ClientContext } from '../components/ClientContext'; import FrontPageImage from '../components/FrontPageImage'; import Footer from '../components/Footer'; -import { NextRouter, withRouter } from 'next/router'; -import { GetStaticProps, InferGetStaticPropsType } from 'next'; +import { GetServerSideProps, InferGetServerSidePropsType, } from 'next'; import { get_data } from './api/directory'; +import { constMatrixArtServer } from '../helpers/matrix_client'; -type Props = InferGetStaticPropsType<typeof getStaticProps> & { +type Props = InferGetServerSidePropsType<typeof getServerSideProps> & { }; type State = { @@ -67,11 +67,28 @@ class Home extends PureComponent<Props, State>{ } Home.contextType = ClientContext; -export const getStaticProps: GetStaticProps = async (context) => { - let image_events: MatrixImageEvents[] = []; +export const getServerSideProps: GetServerSideProps = async (context) => { + context.res.setHeader( + 'Cache-Control', + 'public, s-maxage=10, stale-while-revalidate=59' + ); try { const data = await get_data(); + if (!client?.accessToken) { + try { + let serverUrl = constMatrixArtServer + "/_matrix/client"; + await client?.registerAsGuest(serverUrl); + } catch (error) { + console.error("Failed to register as guest:", error); + return { + props: { + image_events: [] + } + }; + } + } + let image_events: MatrixImageEvents[] = []; // TODO fix this somehow. It is super inefficent. for (let user of data) { // We dont need many events diff --git a/pages/post/[id].tsx b/pages/post/[id].tsx @@ -315,7 +315,21 @@ export const getServerSideProps: GetServerSideProps = async (context) => { if (event_id && event_id.startsWith("$")) { try { const data = await get_data(); - + if (!client?.accessToken) { + try { + let serverUrl = constMatrixArtServer + "/_matrix/client"; + await client?.registerAsGuest(serverUrl); + } catch (error) { + console.error("Failed to register as guest:", error); + return { + props: { + directory_data: data, + event_id: event_id, + hasFullyLoaded: false, + } + }; + } + } // TODO fix this. It is super inefficent. for (let user of data) { // TODO check what happens if this is a non public image.