Skip to content

Commit

Permalink
lazy load Read/Search pages, cache paste requests
Browse files Browse the repository at this point in the history
  • Loading branch information
querwurzel committed Nov 8, 2023
1 parent dfedb77 commit b3849d8
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,14 @@
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.WebExchangeBindException;
import org.springframework.web.server.ResponseStatusException;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.time.LocalDateTime;

import static com.github.binpastes.paste.api.model.ListView.ListItemView;

@RestController
Expand All @@ -55,6 +49,16 @@ public Mono<SingleView> findPaste(@PathVariable("pasteId") String pasteId, Serve
.doOnNext(paste -> {
if (paste.isOneTime()) {
response.getHeaders().add(HttpHeaders.CACHE_CONTROL, "no-store");
} else {
if (paste.getDateOfExpiry() != null ) {
var in5min = LocalDateTime.now().plusMinutes(5);
if (in5min.isAfter(paste.getDateOfExpiry())) {
response.getHeaders().add(HttpHeaders.CACHE_CONTROL, "max-age=" + Duration.between(LocalDateTime.now(), paste.getDateOfExpiry()).toSeconds());
return;
}
}

response.getHeaders().add(HttpHeaders.CACHE_CONTROL, "max-age=300");
}
})
.map(reference -> SingleView.from(reference, remoteAddress(request)))
Expand Down
2 changes: 1 addition & 1 deletion frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="robots" content="noindex, nofollow"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="icon shortcut" type="image/png" href="./favicon.png"/>
<link rel="icon shortcut" type="image/png" href="/favicon.png"/>
</head>
<body id="root">
<script src="./src/index.tsx" type="module" defer="defer" async="async"></script>
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import Footer from './components/Footer/Footer';
import Header from './components/Header/Header';
import RecentPastes from './components/RecentPastes/RecentPastes';
import Create from './pages/Create';
import Read from './pages/Read';
import Search from './pages/Search';

const NotFound = lazy(() => import("./pages/404"));
const Read = lazy(() => import('./pages/Read'));
const Search = lazy(() => import('./pages/Search'));
const NotFound = lazy(() => import('./pages/404'));

const App: () => JSX.Element = () => {
return (
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/api/model/PasteCreateCmd.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

export interface PasteCreateCmd {
export type PasteCreateCmd = {
title?: string
content: string
isEncrypted?: boolean
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/api/model/PasteListView.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

export interface PasteListView {
export type PasteListView = {
id: string
title?: string
sizeInBytes: number
Expand All @@ -8,6 +8,6 @@ export interface PasteListView {
dateOfExpiry?: string
}

export interface PasteList {
export type = PasteList {
pastes: Array<PasteListView>
}
2 changes: 1 addition & 1 deletion frontend/src/api/model/PasteView.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

export interface PasteView {
export type PasteView = {
id: string
title?: string
content: string
Expand Down
10 changes: 7 additions & 3 deletions frontend/src/components/CreatePaste/CreatePaste.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ import {encrypt} from "../../crypto/CryptoUtil";
import {Copy} from '../../assets/Vectors';
import styles from "./createPaste.module.css";

export interface PasteClone {
export type PasteClone = {
title?: string
content: string
}

interface CreatePasteProps {
type CreatePasteProps = {
onCreatePaste: (paste: PasteCreateCmd) => Promise<string>
initialPaste?: PasteClone
}

interface FormModel extends Omit<PasteCreateCmd, "isEncrypted"> {
type FormModel = {
title?: string
content: string
expiry?: 'ONE_HOUR' | 'ONE_DAY' | 'ONE_WEEK' | 'ONE_MONTH' | 'THREE_MONTHS' | 'ONE_YEAR' | 'NEVER'
exposure?: 'PUBLIC' | 'UNLISTED' | 'ONCE'
password: string
}

Expand Down
3 changes: 1 addition & 2 deletions frontend/src/components/Footer/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {JSX} from 'solid-js';

const Footer: () => JSX.Element = () => {

return (
<div>
<span>© 2023</span>
<span>© {new Date().getFullYear()}</span>
</div>
)
}
Expand Down
1 change: 0 additions & 1 deletion frontend/src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {A} from '@solidjs/router';
import {JSX} from 'solid-js';

const Header: () => JSX.Element = () => {

return (
<div>
<h1><A href={'/'}>BinPastes</A></h1>
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/ReadPaste/ReadPaste.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {Component, createEffect, createSignal, JSX, lazy, on, Show} from 'solid-js';
import linkifyElement from 'linkify-element';
import {Component, createEffect, createSignal, JSX, on, Show} from 'solid-js';
import {PasteView} from '../../api/model/PasteView';
import {decrypt} from '../../crypto/CryptoUtil';
import {relativeDiffLabel, toDateString, toDateTimeString} from '../../datetime/DateTimeUtil';
import {Lock, Unlock, Key, Trash, Copy} from '../../assets/Vectors';
import styles from './readPaste.module.css';

interface ReadPasteProps {
type ReadPasteProps = {
paste: PasteView
onClonePaste: () => void
onDeletePaste: () => void
Expand Down
1 change: 0 additions & 1 deletion frontend/src/pages/404.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {A} from '@solidjs/router';
import {JSX} from 'solid-js';

const NotFound: () => JSX.Element = () => {

return (
<div style="margin:1rem">
<h3><i>404</i> Nothing around here :(</h3>
Expand Down
1 change: 0 additions & 1 deletion frontend/src/pages/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {JSX} from 'solid-js';

const Search: () => JSX.Element = () => {

return (
<>
<h2>Search</h2>
Expand Down

0 comments on commit b3849d8

Please sign in to comment.