Skip to content

Beautiful Open-Source Form Builder

perf: allow bounded form list page sizes (#1225)

Julien Nahum5aebe712e5a2
diff --git a/api/app/Http/Controllers/Forms/FormController.php b/api/app/Http/Controllers/Forms/FormController.php
index 4b3394512..821af1291 100644
--- a/api/app/Http/Controllers/Forms/FormController.php
+++ b/api/app/Http/Controllers/Forms/FormController.php
@@ -18,6 +18,7 @@ use App\Service\Forms\FormCleaner;
use App\Service\Storage\FileUploadPathService;
use App\Service\Storage\StorageFileNameParser;
use App\Service\Storage\UploadSecurityService;
+use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
@@ -35,11 +36,13 @@ class FormController extends Controller
$this->formCleaner = new FormCleaner();
}
- public function index(Workspace $workspace)
+ public function index(Request $request, Workspace $workspace)
{
$this->authorize('ownsWorkspace', $workspace);
$this->authorize('viewAny', Form::class);
+ $perPage = min(max($request->integer('per_page', 10), 1), 100);
+
// Select only columns needed for FormListResource (excludes heavy 'properties' and 'removed_properties')
$forms = $workspace->forms()
->select([
@@ -59,7 +62,8 @@ class FormController extends Controller
->withCount(['submissions as submissions_count' => fn ($q) => $q->where('status', FormSubmission::STATUS_COMPLETED)])
->withTotalViews()
->orderByDesc('updated_at')
- ->paginate(10);
+ ->paginate($perPage)
+ ->withQueryString();
return FormListResource::collection($forms);
}
diff --git a/api/tests/Feature/Forms/FormTest.php b/api/tests/Feature/Forms/FormTest.php
index 29f6cd47c..4f97c74a2 100644
--- a/api/tests/Feature/Forms/FormTest.php
+++ b/api/tests/Feature/Forms/FormTest.php
@@ -35,6 +35,42 @@ it('can fetch forms', function () {
->assertJsonPath('data.0.title', $form->title);
});
+it('supports a bounded form list page size', function () {
+ $user = $this->actingAsUser();
+ $workspace = $this->createUserWorkspace($user);
+
+ foreach (range(1, 15) as $index) {
+ $this->createForm($user, $workspace, [
+ 'title' => "Pagination test form {$index}",
+ ]);
+ }
+
+ $this->getJson(route('open.workspaces.forms.index', [
+ 'workspace' => $workspace->id,
+ 'per_page' => 20,
+ ]))
+ ->assertSuccessful()
+ ->assertJsonCount(15, 'data')
+ ->assertJsonPath('meta.per_page', 20);
+
+ $this->getJson(route('open.workspaces.forms.index', [
+ 'workspace' => $workspace->id,
+ 'per_page' => 5,
+ ]))
+ ->assertSuccessful()
+ ->assertJsonPath('meta.per_page', 5)
+ ->assertJsonPath('meta.last_page', 3)
+ ->assertJsonPath('links.next', fn (string $url) => str_contains($url, 'per_page=5'));
+
+ $this->getJson(route('open.workspaces.forms.index', [
+ 'workspace' => $workspace->id,
+ 'per_page' => 1000,
+ ]))
+ ->assertSuccessful()
+ ->assertJsonCount(15, 'data')
+ ->assertJsonPath('meta.per_page', 100);
+});
+
it('returns lightweight form list without properties', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);